jQuery.contains()Method

jQuery 杂项方法jQuery Miscellaneous Methods

Example

Detect whether an element is contained within another element:

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

Try it Yourself »

Definition and Usage

The $.contains() method is used to determine whether the specified element contains another element. That is, it determines whether another DOM element is a descendant of the specified DOM element.


Syntax

$.contains( container, contained )

Parameters Description
container Element type Specifies the ancestor container element that may contain other elements.
contained Element type Specifies the descendant element that may be contained by other elements.

jQuery 杂项方法jQuery Miscellaneous Methods

Other Extensions