Last month I studied "js to determine whether an element is a child element of another element ". I found it quite easy to use, but there are still many flaws in the jQuery application, such as when writing multiple elements It's not very convenient to get up. So I wrote two relatively simple jQuery extensions to determine whether an element is a child element of another element (or itself):
//Judge: whether the current element is a child element of the filtered element
jQuery.fn.isChildOf = function(b){
return (this .parents(b).length > 0);
};
//Judge: 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:
$(document).click(function(event){
alert($(event.target). isChildOf(".floatLayer"));
});
or:
$(document).click(function(event){
alert($(event.target).isChildAndSelfOf (".floatLayer"));
});
Demo address:
http://demo.jb51.net/js/2012/isParent/jquery.htm