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