Because size was abandoned in jquery version 1.8, the size method cannot be used in versions after 1.8; this method can return the number of elements matched by the jquery selector and can be replaced by the length attribute, which contains The number of elements in the jquery object, the syntax is "$().length".
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
The size() method was deprecated in jQuery version 1.8. Please use the length property instead.
The size() method returns the number of elements matched by the jQuery selector.
Syntax
$(selector).size()
The length property contains the number of elements in the jQuery object.
Syntax
$(selector).length
The example is as follows:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>123</title> <script src="js/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ alert($("li").length); }); }); </script> </head> <body> <button>输出li的数量</button> <ul> <li>Coffee</li> <li>Milk</li> <li>Soda</li> </ul> </body> </html>
Output result:
After clicking the button:
Recommended related video tutorials: jQuery video tutorial
The above is the detailed content of Why can't size be used in jquery?. For more information, please follow other related articles on the PHP Chinese website!