开发者

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
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜