简体   繁体   English

在jQuery中选择孩子的孩子

[英]Selecting children of a child in jQuery

I have a side nav that is importing the complete site nav and I'd like to cut it down to only child links of the current section. 我有一个侧面导航,它正在导入整个网站的导航,我想将其缩减为当前部分的仅子链接。 Structure is this: 结构是这样的:

<div class="sidebarLinks">
<ul>
    <li> <!-- MAIN NAV SECTION -->
        <a href="#" id="MainA">MainA</a>
    </li>
    <li> <!-- MAIN NAV SECTION -->
        <a href="#" id="MainB">MainB</a>
        <ul>
            <li><a href="#" id="B-A">B-A</a></li>
            <li><a href="#" id="B-B">B-B</a></li>
            <li><a href="#" id="B-C">B-C</a></li>
        </ul>
    <li>
    <li> <!-- MAIN NAV SECTION -->
        <a href="#" id="MainC">MainC</a>
        <ul>
            <li><a href="#" id="C-A">C-A</a></li>
            <li><a href="#" id="C-B">C-B</a></li>
            <li><a href="#" id="C-C">C-C</a></li>
        </ul>
    <li>
</ul>
</div>

So if I'm on BA, I want to see BA, BB and BC in the side nav, but not MainB or any other links. 因此,如果我在BA上,我希望在侧面导航中看到BA,BB和BC,但不能看到MainB或任何其他链接。 I've tried the following, but it returns the second child of every main nav section (BB, CB, etc.): 我尝试了以下操作,但是它返回了每个主要导航部分(BB,CB等)的第二个孩子:

$(".sidebarLinks ul li:nth-child(2)").children().show();

Any tips are much appreciated. 任何提示,不胜感激。 Thanks. 谢谢。

$(".sidebarLinks > ul > li:nth-child(2) > *").show();

The jQuery child selector is the ">" character. jQuery子选择器是“>”字符。 You can also save the call to .children() with "> *" as that will get any element that is a child of the nth li. 您也可以使用“> *”将调用保存到.children(),因为它将获得作为第n个li的子元素的任何元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM