ドキュメント

Core/size

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Core

size( )

The number of elements in the jQuery object.
This returns the same number as the 'length' property of the jQuery object. However, it is slightly slower, so length should be used instead.

Examples:
Count the divs. Click to add more.

$(document.body).click(function () { $(document.body).append($("<div>"));
var n = $("div").size();
$("span").text("There are " + n + " divs." + "Click to add more.");}).click(); // trigger the click to start

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    $(document.body).click(function () { $(document.body).append($("<div>"));
var n = $("div").size();
$("span").text("There are " + n + " divs." + "Click to add more.");}).click(); // trigger the click to start
  });
  </script>
  <style> body { cursor:pointer; }
 div { width:50px; height:30px; margin:5px; float:left; background:blue; }
 span { color:red; }
 </style>
</head>
<body>
  <span></span>
 <div></div>
</body>
</html>

NameType