开发者

Count the number of <div>

开发者 https://www.devze.com 2023-01-12 19:18 出处:网络
Is it possible usin开发者_JS百科g jQuery to count the number of div elements? I have this code: <div id = \"center\">

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

精彩评论

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