function isDOMContains(parentEle, ele, container){
//parentEle: 要判断节点的父级节点
//ele:要判断的子节点
//container : 二者的父级节点
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的父节点
let parEle = ele.parentNode
while(parEle && parEle !== container) {
if(parEle === parentEle){
return true
}
parEle = parEle.parentNode
}
return false
}
转载:https://blog.csdn.net/rainbow8590/article/details/78330616