DOM:如何检测新的子元素? [英] DOM : How to detect a new child elements?
本文介绍了DOM:如何检测新的子元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想检测到新的div
元素的插入,仅作为父级div
的直接子级.
I want to detect the insertion of a new div
element only as a direct child of the parent div
.
示例:
<div id="parent">
<div id="child1"></div>
<div id="child2"></div>
<div id="newChild">I want this element to be detected</div>
</div>
DOMNodeInserted()
事件对我来说不是一个解决方案,因为它是在每个元素插入时触发的,而不仅仅是子元素.
The DOMNodeInserted()
event is not a solution for me because it is triggered on every element insertion, not just children.
赞:
<div id="parent">
<div id="child1">
<span>I don't want this new element to be detected</span>
</div>
<div id="child2"></div>
<div id="child3"></div>
</div>
推荐答案
假设您的事件处理程序如下所示:
Assume your event handler looks like this:
function(evt) {
// whatever
}
在处理程序内部,evt.relatedNode
是要在其中执行插入的元素.您可以打开它并决定忽略或处理该事件.
Inside the handler, evt.relatedNode
is the element into which the insertion is being performed. You can switch on it and decide to ignore or process the event.
查看实际效果 .
See it in action.
这篇关于DOM:如何检测新的子元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文