Home >Web Front-end >Front-end Q&A >What is the use of jquery contains() method

What is the use of jquery contains() method

青灯夜游
青灯夜游Original
2022-03-01 18:46:053377browse

In jquery, the contains() method is used to determine whether the specified element contains another element, that is, it is used to determine whether another element is a descendant of the specified element. The syntax is "$.contains(ancestor container element ,descendant elements)”.

What is the use of jquery contains() method

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

jquery contains() method

contains() method is used to determine whether the specified element contains another element.

In short, the contains() method is used to determine whether another DOM element is a descendant of the specified DOM element.

Syntax

$.contains( container, contained )
Parameters

Description

container Element type specifies an ancestor container element that may contain other elements.
contained Element type specifies descendant elements that may be contained by other elements.

Return value

The return value of the jQuery.contains() function is of Boolean type. If the specified element contains another element, It returns true, otherwise it returns false.

Description

jQuery.contains() is only used to compare two DOM elements (Element type, not NodeList or other objects). It will search upwards starting from the parent element of the contained element to determine whether it is equal to the container element. If so, it will return true, otherwise it will return false.

Example:

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<script src="js/jquery-1.10.2.min.js"></script>
	</head>
	<body>

		<script>
			$(function() {
				function funcontain(html) {
					document.body.innerHTML += "<br>" + html;
				}
				funcontain($.contains( document.documentElement, document.body )); // true
				funcontain($.contains( document.body, document.documentElement )); // false
			})
		</script>

	</body>
</html>

What is the use of jquery contains() method

[Recommended learning: jQuery video tutorial, Web front-end development video

The above is the detailed content of What is the use of jquery contains() method. 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