.length

length 官网原E文: Number

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

  • version added: 1.0length

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

Example:

Count the divs. Click to add more.

<!DOCTYPE html>
<html>
<head>
  <style>

  body { cursor:pointer; }
  div { width:50px; height:30px; margin:5px; float:left;
        background:green; }
  span { color:red; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
  <span></span>
  <div></div>
<script>$(document.body).click(function () {
      $(document.body).append($("<div>"));
      var n = $("div").length;
      $("span").text("There are " + n + " divs." +
                     "Click to add more.");
    }).trigger('click'); // trigger the click to start</script>

</body>
</html>

Demo:

jQuery 1.6 API 中文版Clove整理、修订