.size()

.size()

.size()Returns: Integerversion deprecated: 1.8, removed: 3.0

Description: Return the number of elements in the jQuery object.

  • version added: 1.0.size()

    • This method does not accept any arguments.

The .size() method is deprecated as of jQuery 1.8. Use the .length property instead.

The .size() method is functionally equivalent to the .length property; however, the .length property is preferred because it does not have the overhead of a function call.

Given a simple unordered list on the page:

<ul>
  <li>foo</li>
  <li>bar</li>
</ul>

Both .size() and .length identify the number of items:

alert( "Size: " + $( "li" ).size() );
alert( "Size: " + $( "li" ).length );

This results in two alerts:

Size: 2

Size: 2

Example:

Count the divs. Click to add more.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>size demo</title>
  <style>
  body {
    cursor: pointer;
    min-height: 100px;
  }
  div {
    width: 50px;
    height: 30px;
    margin: 5px;
    float: left;
    background: blue;
  }
  span {
    color: red;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js" rel="external nofollow" ></script>
</head>
<body>
 
<span></span>
<div></div>
 
<script>
$( document.body )
  .click(function() {
    $( this ).append( $( "<div>" ) );
    var n = $( "div" ).size();
    $( "span" ).text( "There are " + n + " divs. Click to add more." );
  })
 
  // Trigger the click to start
  .click();
</script>
 
</body>
</html>

Demo:

© The jQuery Foundation and other contributors
Licensed under the MIT License.
https://api.jquery.com/size

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部