Count the number of <div>
Is it possible usin开发者_JS百科g jQuery to count the number of div elements?
I have this code:
<div id = "center">
<div class ="name">text text</div>
<div class ="name">text text text ... </div>
<div class ="name">text ...</div>
</div>
And get number: 3
$("#center div").length
will give you the count.
document.getElementsByTagName("div").length
will give you the desired answer
<div id="center">
<div class="name">text text</div>
<div class="name">
<div class="SomeOtherDiv">bla bla</div>
</div>
<div class="name">text ...</div>
</div>
Usage:
$('#center div').length // Result: 4
$('#center > div').length // Result: 3
精彩评论