简体   繁体   English

得到 <li> 带孩子 <ul> 从 <ul> 的孩子 <li>

[英]Get <li> with child <ul> from <ul> child of <li>

I have a very simple example of a menu here: 我这里有一个非常简单的菜单示例:

<ul id="1">
    <li><a href="#">First</a></li>
    <li><a href="#">Second</a>
    <ul id="2">
        <li><a href="#">Second - 1</a></li>
        <li><a href="#">Second - 2</a></li>
        <li><a href="#">Second - 3</a>
        <ul id="3">
            <li><a href="#">Aaa</a></li>
            <li><a href="#">Bbb</a></li>
            <li><a href="#">Ccc</a></li>
        </ul>
        </li>
    </ul>
    </li>
    <li><a href="#">Third</a></li>
</ul>

I need to get the <li> that has a child <ul> and that is a child of <ul> who is a child of <li> , and apply a style to it. 我需要获得<li>有子<ul>这是一个孩子<ul>是谁的孩子<li>和样式应用于它。

I know it sounds complicated, but in the example above I want to get only the <li> that says "Second - 3" which is inside a ul, which is a child of a li and has a child ul. 我知道这听起来很复杂,但是在上面的示例中,我只想获取<li>并说“ Second-3”,该词位于ul内,它是li的孩子,并且有ul子。 I don't want to get any other <li> s. 我不想得到任何其他<li>

I can't do this without getting also the li which says "Second", and I don't want that. 我不能没有得到说“ Second”的锂而这样做,我也不想那样。

$("li > ul").addClass('whatever');

Use $("li ul li:has(ul)") 使用$("li ul li:has(ul)")

eg: 例如:

$(function(){
    var items = $("li ul li:has(ul)");
    alert(items.html());
});

Working example: http://jsfiddle.net/EXzaa/ 工作示例: http : //jsfiddle.net/EXzaa/

Try this: 尝试这个:

$("li > ul > li").each(function(){
    if ( $(this).find("ul").length > 0){
        $(this).css({"font-weight":"bold"});
    }
});

Unless I get you wrong this is simple. 除非我弄错了,否则这很简单。 Try something like this: 尝试这样的事情:

$('#3').parent('li').addClass('whatever');

This will select the parent node of the ul element with the id = 3 (only if it is an li element) 这将选择id = 3的ul元素的父节点(仅当它是li元素时)

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

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