JavaScript determines that an element is a child of another element

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.