简体   繁体   English

jQuery: UL select 父母 li 只有孩子

[英]jQuery: UL select parent li who have children only

This has been boggling my mind all afternoon, how do I get my jQuery statement below to only select parent <li> who have children?整个下午这一直让我难以置信,我如何才能将下面的 jQuery 声明仅发送给有孩子的 select 父母<li>

To put it simply, I need to add a css class to <li> items 6, 4 & 8 because they have children directly below them.简单地说,我需要在<li>项目 6、4 和 8 中添加一个 css class,因为它们的正下方有孩子。 My statement goes too deep, it adds the redColor class to li's 7, 10, 9, 11, 12..我的说法太深入了,它在 li 的 7、10、9、11、12 中添加了 redColor class。

$(document).ready(function () {
   $("#test-list li:has(ul)").addClass("redColor");
 });

<ul id="test-list">
 <li id="listItem_1"> <strong>Item 1</strong> </li>
 <li id="listItem_2"> <strong>Item 2</strong> </li>
 <li id="listItem_3"> <strong>Item 3</strong> </li>
 <li id="listItem_5"> <strong>Item 5</strong> </li>
 <li id="listItem_6"> <strong>Item 6</strong>
    <ul>
        <li id="listItem_4"> <strong>Item 4</strong>
            <ul>
                <li id="listItem_7"> <strong>Item 7</strong> </li>
                <li id="listItem_10"> <strong>Item 10</strong> </li>
            </ul>
        </li>
    </ul>
 </li>
 <li id="listItem_8"> <strong>Item 8</strong>
    <ul>
        <li id="listItem_9"> <strong>Item 9</strong> </li>
        <li id="listItem_11"> <strong>Item 11</strong> </li>
        <li id="listItem_12"> <strong>Item 12</strong> </li>
    </ul>
 </li>
</ul>

View on jfiddle as well: http://jsfiddle.net/t6a6G/9/查看 jfiddle 以及: http://jsfiddle.net/t6a6G/9/

Thank you in advance!先感谢您!

If you look closely, you'll see that your jQuery selector is actually working correctly, and only li's 6, 4, and 8 have the class you're adding.如果您仔细观察,您会发现您的 jQuery 选择器实际上工作正常,并且只有 li 的 6、4 和 8 具有您要添加的 class。

The reason the other li's are turning red is that the default color is inherit , the same color as your parent.其他 li 变成红色的原因是默认颜色是inherit ,与您的父母颜色相同。 So setting an li to have color: red also makes its descendants red, unless there's another style that applies to them.因此,将 li 设置为具有color: red也会使其后代变为红色,除非有另一种适用于它们的样式。

Adding a style like li { color: black; }添加像li { color: black; }这样的样式li { color: black; } will make it more obvious that your class is indeed only applied to the correct list items. li { color: black; }将使您的 class 确实仅适用于正确的列表项更明显。

Your selector is just fine.你的选择器很好。 It's adding the redColor class correctly, but the children inherit the color of their parents, so they turn red as well.它正确地添加了redColor class,但是孩子们继承了父母的颜色,所以他们也变成了红色。

You should override this behaviour by explicitly setting the color of <li> s that don't have the redColor class:您应该通过显式设置不具有redColor class 的<li>的颜色来覆盖此行为:

li {
    color:black;
}

See ammended fiddle: http://jsfiddle.net/t6a6G/12/ .见修正小提琴: http://jsfiddle.net/t6a6G/12/

Instead of putting spaces between two things in the CSS, use a ">".不要在 CSS 中的两个事物之间放置空格,而是使用“>”。 So "A > B" means that B is directly a child of A. Also, you were adding the redColor class to everything in the li, and that also includes the ul inside the li.所以“A > B”意味着B直接是A的孩子。此外,您将redColor class添加到li中的所有内容,并且还包括li中的ul。 I think you just wanted the strong tags to be red.我想你只是想让强标签是红色的。

$(document).ready(function() {
// Trying to add css class to li who are parents of children
// When working correctly only items 6, 4, and 8 would be red
    $("#test-list > li:has(ul) > strong").addClass("redColor");
});

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

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