JQuery: How to find out how many children an element has?

此生再无相见时 提交于 2019-12-19 17:44:52

问题


How can I use jQuery to find out how many children an element has?

Say I have the following structure:

<div id="container">
   <div id="column1">
      <div id="asset1"></div>
      <div id="asset2"></div>
   </div>
   <div id="column2">
      <div id="asset1"></div>
      <div id="asset2"></div>
   </div>
</div>

I want to find out how many children the div element: container, has. In this case it would return 2...


回答1:


Use children and length:

$("#container").children().length



回答2:


Use the direct children selector (>) and the length property:

$('#container > *').length

Example - http://jsfiddle.net/TtV8d/



来源:https://stackoverflow.com/questions/10206476/jquery-how-to-find-out-how-many-children-an-element-has

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!