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!