开发者

jQuery list hide/show based on current class

I want to开发者_Go百科 display the entire ul li struct that has a class current somewhere in it

Every other ul li I want to hide. EXCEPT The top level.

My jQuery is not doing what I want, any suggestions?

$("ul li").find("ul").hide();
$("ul:has(li.current)").parents().show();
$("ul:has(li.current)").children().show();

For example:

<ul>
    <li>Entire struct is displayed
        <ul>
            <li>1.1
                 <ul>
                     <li class="current">1.1.1</li>
                 </ul>
            </li>
            <li>1.2</li>
        </ul>
    </li>
    <li>Only this li is shown
        <ul>
            <li>2.1</li>
            <li>2.2</li>
        </ul>
    </li>
</ul>

Displays as:

  • Entire struct is displayed
    • 1.1
      • 1.1.1
    • 1.2
  • Only this li is shown

EDIT: added children to jQuery, but still not working as desired.


The following worked for me..

$("ul").find("ul").hide();
$("ul > li.current").find("ul").show().end().parents().show();

http://jsfiddle.net/uHyPS/5/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜