js判断是由含有a节点_怎么判断某个dom节点是否包含某个dom节点?
.center {
display: flex;
justify-content: center;
align-items: center;
}
.main {
width: 300px;
height: 300px;
background: red;
}
.con {
width: 200px;
height: 200px;
background: green;
}
.text {
width: 100px;
height: 100px;
background: yellow;
}
測試
var main = document.querySelector(".main");
var con = document.querySelector(".con");
var text = document.querySelector(".text");
text.addEventListener("click", function(e) {
console.log(main.contains(e.target));
console.log(con.contains(e.target));
console.log(main.contains(con));
console.log(con.contains(main));
})
console.log(isChildOf(con, main));
console.log(isChildOf(text, main));
console.log(isChildOf(main, con));
function isChildOf(child, parent) {
var parentNode;
if(child && parent) {
parentNode = child.parentNode;
while(parentNode) {
if(parent === parentNode) {
return true;
}
parentNode = parentNode.parentNode;
}
}
return false;
}
總結
以上是生活随笔為你收集整理的js判断是由含有a节点_怎么判断某个dom节点是否包含某个dom节点?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ffmpeg实现摄像头拉流_[FFmpe
- 下一篇: python制作图片数据集_Pytorc