function isDOMContains(parentEle, ele, container){
//parentEle: 要判斷節(jié)點(diǎn)的父級(jí)節(jié)點(diǎn)
//ele:要判斷的子節(jié)點(diǎn)
//container : 二者的父級(jí)節(jié)點(diǎn)
if(parentEle === ele){
return true
}
if(!ele || !ele.nodeType || ele.nodeType !== 1){
return false;
}
//如果瀏覽器支持contains
if(parentEle.contains){
return parentEle.contains(ele)
}
//火狐支持
if(parentEle.compareDocumentPosition){
return !!(parentEle.compareDocumentPosition(ele)&16);
}
//獲取ele的父節(jié)點(diǎn)
let parEle = ele.parentNode
while(parEle && parEle !== container) {
if(parEle === parentEle){
return true
}
parEle = parEle.parentNode
}
return false
}
轉(zhuǎn)載:https://blog.csdn.net/rainbow8590/article/details/78330616