开发者

Counting number of nested elements with JQuery

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜