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.htmStatement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn