In jQuery, determine whether an element is a child element of another element (or its own) _ jquery

Source: Internet
Author: User
I wrote a relatively simple jQuery statement to determine whether an element is a child element of another element (or its own) 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:

The Code is as follows:


// 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:

The Code is as follows:


$ (Document). click (function (event ){
Alert(objects (event.tar get). isChildOf (". floatLayer "));
});


Or:

The Code is as follows:


$ (Document). click (function (event ){
Alert(objects (event.tar get). isChildAndSelfOf (". floatLayer "));
});


Demo address: http://demo.jb51.net/js/2012/isParent/jquery.htm

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.