JavaScript determines that an element is a sub-element of another element uses a lot of, the most common is when you click on the blank space to perform certain operations, such as pop-up layer.
function Isparent (obj,parentobj) {while (obj! = undefined && obj! = null && obj.tagName.toUpperCase ()! = ' BODY ') {if (obj = = parentobj) {return true;} obj = Obj.parentnode;} return false;} $ (document). Click (Function (event) {alert (Isparent (Event.target, $ (". Floatlayer") [0]);});
It feels good to use, but there are still a lot of flaws in the jquery application, such as when multiple elements are not very convenient to write. So write a simpler jquery to determine whether an element is a two extension of another element's child element (or itself):
jquery Code
Determines whether the current element is a child of the filtered element
JQuery.fn.isChildOf = function (b) {return (this.parents (b). length > 0);};
Determines whether the current element is a child of the element being filtered or is 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");});
JavaScript determines that an element is a child of another element