Node.contains detects whether an element node is a child of another element

Source: Internet
Author: User

It is a common thing to detect whether an element node is a child of another element in web development, which makes it easier for us to judge method events, yet many people do not know the element.classlist API, For example, classname management requires a specific JavaScript toolkit to complete, but a method such as detecting whether an element node is a child of another element can only be done by means of a tool or a cyclic detection contains, in fact the following code can:

The code is as follows Copy Code
function (ParentNode, Childnode) {
If (' contains ' in parentnode) {
Return Parentnode.contains (Childnode);
}
else {
Return parentnode.comparedocumentposition (childnode)% 16;
}
}

You'll notice our check contains before using it, you might think, with little known comparedocumentposition in this case, contains not supported (Firefox < 9). This approach would be useful when creating a list between a drag-and-drop control and a determined action.

Add: How jquery determines whether the current element is a child of a node

The code is as follows Copy Code

$ (node). Click (function () {
if ($ (this). Parents ('. AA '). Length > 0) {
is a sub-node under AA class
}
});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.