简体   繁体   中英

How to check element is child of specific element?

If I get elementA and elementB with document.querySelector , how to can I check if elementA is a child element of elementB without jQuery ?

Considering elementA maybe not first level child of elementB .

Exemple: http://jsbin.com/jijoqicozo/edit?html,js,output

You can use B.contains(A) to determine if element A is a descendent of element B.

Note that node.contains is inclusive, meaning that B.contains(B) === true

https://developer.mozilla.org/en/docs/Web/API/Node/contains

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM