Does jquery have a size method?

青灯夜游
Release: 2022-03-23 16:52:55
Original
1880 people have browsed it

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.

Does jquery have a size method?

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>
Copy after login

Does jquery have a size method?

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 ").length

To 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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template