繁体   English   中英

IE11挂起MutationObserver

[英]MutationObserver hangs in IE11

我正在使用MutationObserver检查何时删除了某些节点,并用元素的其他新节点替换了这些节点。

以下代码在Chrome中可以正常运行,但是在IE11上它只是挂起。

如果我更改了removeedNodes的addingNodes检查,则它可以在IE11上运行。 我只是不明白为什么在检查要添加的新节点时它会挂起。

任何想法? 我找不到此问题的任何资源。

 var nodeToObserve = document.querySelector('#targetNode'); var callback = function(mutations, observer) { for (var index = 0; index < mutations.length; index) { var mutation = mutations[index]; if (mutation.type === 'childList' && mutation.addedNodes.length > 0) { console.log(mutation); break; } } observer.disconnect(); } const observer = new MutationObserver(callback); observer.observe(nodeToObserve, { childList: true, // target node's children subtree: true // target node's descendants }); 
 html, body { height: 100%; } #targetNode { border: 1px solid black; height: 100%; } .childNode { //height: 20px; background-color: blue; margin: 10px; padding: 10px; } .grandChildNode { height: 20px; background-color: red; margin: 10px; } 
 <div id="targetNode"> </div> 

您不会在for循环中增加index 结果可能会以不同的顺序出现,具体取决于浏览器,因此if语句将在某些浏览器上触发,但在其他浏览器上不会触发。 因此, if不执行无限循环b / c的if语句,系统将挂起。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM