Home >Web Front-end >Front-end Q&A >Does jquery have a size method?
There is a size() method before jquery version 1.8, but there is no size() method in versions 1.8 and later. The size() method is used to return the number of elements matched by the jQuery selector. It was abandoned in jQuery version 1.8. The length attribute can be used instead.
The operating environment of this tutorial: windows7 system, jquery3.6.0 version, Dell G3 computer.
The size() method is used to return the number of elements matched by the jQuery selector.
Lower versions of jquery (versions before jquery 1.8) have a size method, while higher versions of jquery do not have a size method.
The size() method is deprecated in jQuery version 1.8. Please use the length
attribute instead.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script> <script> $(document).ready(function() { $("button").click(function() { alert($("li").size()); }); }); </script> </head> <body> <button>输出li元素的数量</button> <ul> <li>Coffee</li> <li>Milk</li> <li>Soda</li> </ul> </body> </html>
Higher versions of jquery cannot use the size() method and will report an error.
The difference between jQuery length and size()
size() is a function provided by jQuery, and length is a property (without brackets).
size() is a method for operating object instances. It is defined in jQuery and can only be used under jQuery; length is a property, defined in dom and javascript, and can be used without jQuery.
For tag object elements, for example, counting how many paragraph elements there are in the html page
, then $("p").size()==$("p ").lengthTo calculate the length of a string or the number of elements in an array, you can only use length instead of size()
Explanation:
.size() This method does not accept any parameters
[Recommended learning: jQuery video tutorial, web front-end development]
The above is the detailed content of Does jquery have a size method?. For more information, please follow other related articles on the PHP Chinese website!