Home > Web Front-end > JS Tutorial > Determine whether an element is a child element of another element (or itself) in jQuery_jquery

Determine whether an element is a child element of another element (or itself) in jQuery_jquery

WBOY
Release: 2016-05-16 17:55:11
Original
1335 people have browsed it

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

Copy code The code is as follows:

//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:
Copy code The code is as follows:

$(document).click(function(event){
alert($(event.target). isChildOf(".floatLayer"));
});

or:
Copy code The code is as follows:

$(document).click(function(event){
alert($(event.target).isChildAndSelfOf (".floatLayer"));
});

Demo address: http://demo.jb51.net/js/2012/isParent/jquery.htm
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template