简体   繁体   English

JQuery,检索孩子和孙子

[英]JQuery, retrieve children and grandchildren

Let's say I have a JQuery object which has (or points to) such a structure: 假设我有一个JQuery对象,它具有(或指向)这样的结构:

<div id="parent">
  <div id="child1">
    <div id="grandchild1">
      // ... grandchild can also have children
    </div>
    // ... and so on of grandchildren
  </div>
  <div id="child2">

  </div>
  // .... and so on
</div>

Is there possibility to get plain array of elements (JQuery object) from my object like this: 是否有可能从我的对象获取简单的元素数组(JQuery对象),如下所示:

['parent', 'child1', 'child2', ..., 'grandchild1', ...]

Thanks 谢谢

PS Please notice that it can't be selector something like this $('div > div'), because I already have JQuery object and only from this object I need take stuff. PS请注意它不能是这样的选择器$('div> div'),因为我已经有了JQuery对象,只有从这个对象我需要拿东西。

You can retrieve children and grandchildren using the * selector. 您可以使用*选择器检索子孙。 The items will be returned in tree order. 这些项目将按树顺序返回。 You can then get a plain array of elements using jQuery.toArray() : 然后,您可以使用jQuery.toArray()获取一个简单的元素数组:

 $(function() { var a = $("#wrapper").find("*").toArray(); console.log(a); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id="wrapper"> <div id="parent"> <div id="child1"> <div id="grandchild1"> <div class="level-4"></div> <div class="level-4"></div> <div class="level-4"></div> </div> </div> <div id="child2"> <div class="level-3"></div> <div class="level-3"></div> <div class="level-3"></div> <div class="level-3"> <div class="level-4"></div> <div class="level-4"></div> <div class="level-4"></div> </div> </div> </div> </div> 

Following is an overkill but useful if you are more interested in iterating the hierarchy using recursion: 如果您对使用递归迭代层次结构更感兴趣,则以下内容非常有用但很有用:

 function recursiveIterate($node, array) { $node.children().each(function() { array.push(this); recursiveIterate($(this), array); }); } $(function() { var a = []; recursiveIterate($("#wrapper"), a); console.log(a); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id="wrapper"> <div id="parent"> <div id="child1"> <div id="grandchild1"> <div class="level-4"></div> <div class="level-4"></div> <div class="level-4"></div> </div> </div> <div id="child2"> <div class="level-3"></div> <div class="level-3"></div> <div class="level-3"></div> <div class="level-3"> <div class="level-4"></div> <div class="level-4"></div> <div class="level-4"></div> </div> </div> </div> </div> 

You can use the toArray() [docs] method to make your jQuery object into an Array. 您可以使用toArray() [docs]方法将jQuery对象转换为Array。

var arr = my_obj.toArray();

...or the get() [docs] method if you don't pass it any arguments. ...或者get() [docs]方法,如果你没有传递任何参数。

var arr = my_obj.get();

If I've misunderstood, and you're pointing to the root of the structure, and need to get its descendants, use the find() [docs] method method. 如果我误解了,并且您指向结构的 ,并且需要获取其后代,请使用find() [docs]方法方法。

var descendants = my_obj.find('div');

jQuery has many traversal methods that let you get around the DOM structure from a given point. jQuery 有许多遍历方法 ,可以让你从给定的点开始绕过DOM结构。


If you only wanted children and grandchildren, and nothing deeper, do this: 如果你想要孩子和孙子,而不是更深层次,那就这样做:

var two_generations = my_obj.find('> div > div');

This will give you div elements only 2 generations deep. 这将给你div元素只有2代深。

If you don't want to limit it to div elements, do this: 如果您不想将其限制为div元素,请执行以下操作:

var two_generations = my_obj.find('> * > *');

Hey man I'm pretty new here and I was having a similar brain fart. 嘿,我在这里很新,我有一个类似的脑屁。 To get the grandchildren, I suggest using the .children() approach: 为了得到孙子,我建议使用.children()方法:

$('#parent') //selects the parents
$('#parent').children() //selects the children
$('#parent').children().children() //selects the grandchildren

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

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