我正在使用MutationObserver检查何时删除某些节点并将其他新节点替换为元素。
以下代码在Chrome中完全正常,但在IE11上它只是挂起。
如果我使用removedNodes更改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>
你没有在你的index
循环中增加for
。可能结果会以不同的顺序显示,具体取决于浏览器,因此if
语句将在某些浏览器上触发,但不会在其他浏览器上触发。因此,当if
语句未执行无限循环的b / c时,系统将挂起。