Home  >  Article  >  Web Front-end  >  What is the difference between length and size() in jQuery?

What is the difference between length and size() in jQuery?

伊谢尔伦
伊谢尔伦Original
2017-06-17 09:12:552435browse

size()Function is used to return the number of elements encapsulated by the current jQuery object. The size() function has the same effect as the length attribute. This function belongs to the jQuery object (instance). The return value of the size() method is consistent with the length property of the jQuery object.

Grammar structure:

$(selector).size()

Example code:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.php.cn/" />
<title>php.cn</title>
<style type="text/css">
span{color:red;}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript"> 
$(document).ready(function(){ 
  $("#btn").click(function(){ 
    $("span").text($("li").size()); 
  }) 
})
</script>
</head>
<body>
<div>
  <ul>
    <li>后台专区</li>
    <li>前台专区</li>
    <li>数据库专区</li>
    <li>站长交流</li>
  </ul>
</div>
<div>li元素集合中li元素的数量:<span></span></div>
<button id="btn">点击查看实例</button>
</body>
</html>

The difference between jQuery length and size() is summarized as follows:
1.length is an attribute, size( ) is the method. For tag object elements, such as counting how many paragraph elements there are in the html page e388a4556c0f65e1904146cc1a846bee94b3e26ee717c64999d7867364b1b4a3 then $("p").size() == $("p").length

2. If you just want to get the number of elements, the effect is the same. The values ​​obtained by $("img").length and $("img").size() are the same; but if you want to get The length of string can only be determined by length, such as $("#text").val().length

Calculate the length of a string or calculate an arrayThe number of elements, then you can only use length instead of size() at this time

The difference between jQuery length and size() From the above, it can be seen that size() is implemented by calling the length attribute, and in jquery 1.8 Later, length replaced size(), which is better because length does not need to return a function call.

Code example:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="../js/jquery-1.4.2.js">
</script>
<script type="text/javascript">
$(function(){
    var imglength=$("img").length;   //2
    var imgsize=$("img").size();   //2
    var vallength=$("#text").val().length; //3
})
</script>
</head>
<body>
<img src="test1.jpg"/>
<img src="test2.jpg"/>
姓名<input type="text" id="text" value="aaa"/>
<!--
jQuery length和size()区别总结如下:
1.length是属性,size()是方法。
2.如果你只是想获取元素的个数,两者效果一样既 $("img").length 和 $("img").size() 获取的值是一样的;
但是如果是获取字符串的长度就只得用length, 如 $("#text").val().length
 
从上可以看出size()是调用length属性实现的,而且在jquery 1.8后 length取代了 size()  ,因为length不需要返回一个函数调用,更优秀。
 -->
</body>
</html>

The above is the detailed content of What is the difference between length and size() in jQuery?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn