开发者

Counting number of nested elements with JQuery

开发者 https://www.devze.com 2023-03-24 18:59 出处:网络
I want to konw how can I count the child elements under a given Jquery object. I know that \'length\' and \'size()\' returns the amount of children elements, 开发者_如何学Cbut I need a function that a

I want to konw how can I count the child elements under a given Jquery object. I know that 'length' and 'size()' returns the amount of children elements, 开发者_如何学Cbut I need a function that also count the children's children and so on. So if their is a 'span' tag, nested inside another 'span' and another 'span', the function should return 3.


For a specific element type:

$('#my_element').find('span').length

For all elements with a specific class:

$('#my_element').find('.my_class').length

For all elements inside an element:

$('#my_element').find('*').length

Working example: http://jsfiddle.net/AlienWebguy/whxf2/


Use $([selector]).find('*') to get all children of the element.

0

精彩评论

暂无评论...
验证码 换一张
取 消