加载中...

length


概述    length

返回值:Number

描述:在jQuery对象中元素的数量。

目前匹配的元素数量。.size()方法将返回相同的数字。

示例

实例

计算 div 数量,点击后会增加一个 div。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. body { cursor:pointer; }
  6. div { width:50px; height:30px; margin:5px; float:left;
  7. background:green; }
  8. span { color:red; }
  9. </style>
  10. <script src="http://code.jquery.com/jquery-latest.js"></script>
  11. </head>
  12. <body>
  13. <span></span>
  14. <div></div>
  15. <script>$(document.body).click(function () {
  16. $(document.body).append($("<div>"));
  17. var n = $("div").length;
  18. $("span").text("There are " + n + " divs." +
  19. "Click to add more.");
  20. }).trigger('click'); // trigger the click to start</script>
  21. </body>
  22. </html>

运行一下


还没有评论.