打印出 div#wrap
里的直接子元素 div
<div id="wrap">
<div class="div1">
<span>div1>span1</span>
<span>div1>span2</span>
<p>
div1>p
<span>div1>p>span</span>
</p>
<div>div1>div</div>
</div>
<p class="p1">
<p>p>p1</p>
<p>p>p2</p>
</p>
<div class="div2">div2</div>
</div>
let wrap = document.getElementById('wrap');
wrap.onclick = function (e) {
let target = e.target;
while (target !== wrap) {
if (target.nodeName === 'DIV' && target.parentNode === wrap) {
console.log(target);
break;
}
target = target.parentNode
}
}
网友评论