Home  >  Article  >  Web Front-end  >  How to determine whether objects are the same in jquery

How to determine whether objects are the same in jquery

青灯夜游
青灯夜游Original
2022-03-16 18:33:382587browse

In jquery, you can use the is() method to determine whether the objects are the same. This method can check whether the selected element object matches the selector or jQuery object; the syntax is "jQuery object 1.is(jQuery object 2 )", returns true if the two objects are the same.

How to determine whether objects are the same in jquery

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

In jquery, you can use the is() method to determine whether the objects are the same.

Judge whether two jQuery objects are the same

Use the is() method provided by jQuery. Note that you cannot directly Use === to judge, even if it is the same element, it is false, because two jQuery objects

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<script src="js/jquery-1.10.2.min.js"></script>
		<script>
			$(document).ready(function() {
				/*
				  判断元素是否相同, 使用 is() 方法
				  不能直接 === 判断, 其实不是同一份 jquery 对象
				 */
				let container1 = $(&#39;.container&#39;)
				let container2 = $(&#39;.container&#39;)

				// false
				console.log(container1 === container2);
				// true
				console.log(container1.is(container2));
			});
		</script>
	</head>
	<body>
		<div class="container">hello</div>
	</body>
</html>

How to determine whether objects are the same in jquery

# are constructed ##Description:

is() detects a set of matching elements based on a selector, element, or jQuery object, and returns true if at least one of these elements matches the given parameters.

Syntax:

$(selector).is(filter)

ParametersDescriptionString value containing the selector expression that matches the element.
filter
[Recommended learning:

jQuery video tutorial, web front-end

The above is the detailed content of How to determine whether objects are the same 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