简体   繁体   English

什么时候调用“ DOMNodeInserted”事件?

[英]When is “DOMNodeInserted” event called?

DOMNodeInserted event is called when the node "be appended to", or "be appended"? 当节点“附加到”或“附加”时,将调用DOMNodeInserted事件吗?

I ask this because the following code: 我问这是因为以下代码:

function AddTextToContainer () {
    var textNode = document.createTextNode ("My text");
    var container = document.getElementById ("container");

    if (container.addEventListener) {
        container.addEventListener ('DOMNodeInserted', OnNodeInserted, false);
    }
    container.appendChild (textNode);
}

and that: 然后:

function AddTextToContainer () {
   var textNode = document.createTextNode ("My text");
   var container = document.getElementById ("container");

   if (textNode.addEventListener) {
       textNode.addEventListener ('DOMNodeInserted', OnNodeInserted, false);
   }
   container.appendChild (textNode);
}

Both invoke OnNodeInserted in Chrome. 两者都在Chrome中调用OnNodeInserted Is it a bug? 是虫子吗?

This is from W3C 这是来自W3C

 DOMNodeInserted Fired when a node has been added as a child of another node. This event is dispatched after the insertion has taken place. The target of this event is the node being inserted. Bubbles: Yes Cancelable: No Context Info: relatedNode holds the parent node 

The key is the Bubbles: Yes - thats why its being fired on the container as well. 关键是气泡:是的 -这就是为什么它也要在容器上射击的原因。

If you want to prevent the event from bubbling up just use event.stopPropagation(); 如果要防止事件冒泡,请使用event.stopPropagation();。 in your text node callback. 在您的文本节点回调中。 Events are then no longer handled up the dom tree. 然后,事件不再在dom树上处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用“DOMNodeInserted”事件时添加元素 - Add an element when “DOMNodeInserted” event called DOMNodeInserted事件循环 - DOMNodeInserted event loop 当 DOMNodeInserted 事件触发时, upateCount function 会按预期工作,但当 DOMNodeRemoved 触发时不会再次针对相同的 function? - When DOMNodeInserted event is fire upadateCount function works as expected but not when DOMNodeRemoved is fire which again target the same function? 在Document.ready中将Bind事件注册为DOMNodeInserted - Registering Bind event as DOMNodeInserted in document.ready DOMNodeInserted / Removed事件Polyfill(或类似事件) - DOMNodeInserted/Removed event polyfills (or similar events) DOMNodeInserted事件在IE中多次触发 - DOMNodeInserted event gets fire multiple times in IE 将一系列 DOMNodeInserted 事件作为单个事件处理 JavaScript - Handling a series of DOMNodeInserted events as a single event in JavaScript JavaScript-检测点击事件是否触发了DOMNodeInserted事件 - JavaScript - Detect whether a click event triggered a DOMNodeInserted event 如何在使用jQuery注入HTML后触发DOMNodeInserted事件 - How to trigger the DOMNodeInserted event after injecting HTML with jQuery 事件监听器DOMNodeInserted被多次运行,为什么? - Event listener DOMNodeInserted being run multiple times, why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM