Last month, the study of "JS to determine whether an element is another element of the child", it feels pretty good, but in the jquery application still have a lot of flaws, such as multiple elements of time is not very convenient to write. So write a simpler jquery to determine whether an element is a child element of another element (or itself) with two extensions:
Copy Code code as follows:
Determines whether the current element is a child element of the filtered element
JQuery.fn.isChildOf = function (b) {
Return (This.parents (b). length > 0);
};
Determines 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 code as follows:
$ (document). Click (Function (event) {
Alert ($ (event.target). Ischildof (". Floatlayer"));
});
Or:
Copy Code code as follows:
$ (document). Click (Function (event) {
Alert ($ (event.target). Ischildandselfof (". Floatlayer"));
});
Demo Address: http://demo.jb51.net/js/2012/isParent/jquery.htm