【发布时间】:2019-02-13 01:21:09
【问题描述】:
我正在使用 MutationObserver 来检查某些节点何时被删除并被其他新节点替换为元素。
以下代码在 Chrome 中运行良好,但在 IE11 上它只是挂起。
如果我用 removeNodes 更改 addedNodes 检查,它可以在 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>
【问题讨论】:
-
在 IE11 V11.228.17134.0 和 win10 上运行
标签: javascript jquery internet-explorer-11 mutation-observers