定位所选父li的所有嵌套li

时间:2013-02-22 08:38:16

标签: jquery

我有三个无序列表互相嵌套。第二个和第三个ul被隐藏(display: none;

首先,我需要显示ul.second的{​​{1}}的所有li元素。

根据第二个事件,我需要显示ul.first ul.third的所有li元素。 这是代码:

HTML

ul.second

CSS

<ul class="first">
<li class="first-element">
    <ul class="second">
        <li class="second-element">
            <ul class="third">
                <li class="third-element"></li>
                <li class="third-element"></li>
                <li class="third-element"></li>
            </ul>
        </li>
        <li class="second-element">
            <ul class="third">
                <li class="third-element"></li>
                <li class="third-element"></li>
                <li class="third-element"></li>
            </ul>
        </li>
        <li class="second-element">
            <ul class="third">
                <li class="third-element"></li>
                <li class="third-element"></li>
                <li class="third-element"></li>
            </ul>
        </li>
    </ul>
</li>
<li class="first-element">
    <ul class="second">
        <li class="second-element">
            <ul class="third">
                <li class="third-element"></li>
                <li class="third-element"></li>
                <li class="third-element"></li>
            </ul>
        </li>
        <li class="second-element">
            <ul class="third">
                <li class="third-element"></li>
                <li class="third-element"></li>
                <li class="third-element"></li>
            </ul>
        </li>
        <li class="second-element">
            <ul class="third">
                <li class="third-element"></li>
                <li class="third-element"></li>
                <li class="third-element"></li>
            </ul>
        </li>
    </ul>
</li>
</ul>

JS

ul.second li {
    display: none;
}
ul.third li {
    display: none;
}

如何以其他方式定位$('li.first-element:eq(0) li.second-element').show(); // works fine, it shows only li.second-element of ul.first first li $('li.second-element:eq(0) li.third-element').show(); // does not work, in log it gives me empty []

1 个答案:

答案 0 :(得分:0)

然后

$('.first li:eq(0) .second > li');
$('.second > li:eq(0) .third > li');