JQuery determines whether an element is a child element of another element (or itself)

Source: Internet
Author: User

Last month, I studied "js determining whether an element is a child element of another element". It seems quite useful, but there are still many defects in jQuery applications, for example, it is not convenient to write multiple elements. Therefore, I wrote a simple jQuery statement to determine whether an element is two extensions of the child element (or its own) of another element:

Copy to ClipboardReference: [www.bkjia.com]
// Determine whether the current element is a child element of the filtered element.
JQuery. fn. isChildOf = function (B ){
Return (this. parents (B). length> 0 );
};
// Determine whether the current element is a child element of the filtered element or itself
JQuery. fn. isChildAndSelfOf = function (B ){
Return (this. closest (B). length> 0 );
};

It is also very convenient to use:

Copy to ClipboardReference: [www.bkjia.com]
$ (Document). click (function (event ){
Alert(objects (event.tar get). isChildOf (". floatLayer "));
});

Or:

Copy to ClipboardReference: [www.bkjia.com] $ (document). click (function (event ){
Alert(objects (event.tar get). isChildAndSelfOf (". floatLayer "));
});

Demo:

<! DOCTYPE html> 

Tip: the code can be modified before running!

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.