选择儿童但不是孙子与Jquery [英] Choosing Children but not grandchildren with Jquery

查看:119
本文介绍了选择儿童但不是孙子与Jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套的无序列表,像这样:

 < ul id =parent> 
< li>
< ul> <! - 选择此 - >
< li>< / li>
< li>
< ul>
< li>< / li>
< / ul>
< / li>
< / ul>
< li>
< / ul>

我需要选择#parent的子项,但不是子孙。



$(#parent).children('ul'); 选择所有子级,包括孙子。

  $('#parent')。children('> ul'); 





您希望子选择器



您需要以下内容:

  $('#parent> li> ul'); 


I have a nested unordered list like so:

<ul id="parent">
   <li>
      <ul> <!-- select this-->
        <li></li>
        <li>
            <ul>
                <li></li>
            </ul>
        </li>
      </ul>
   <li>
</ul>

I need to select the children of #parent but not the grandchildren.

$(#parent).children('ul'); selects all children, including grandchildren. How do I limit this to just children?

解决方案

$('#parent').children('>ul');

The above would not work with the markup in the question.

You want the child selector

You'd need the following:

$('#parent > li > ul');

这篇关于选择儿童但不是孙子与Jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆