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)”.
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>
[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!