【问题标题】:MutationObserver hangs in IE11MutationObserver 在 IE11 中挂起
【发布时间】: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


【解决方案1】:

您没有在for 循环中增加index。结果可能会根据浏览器以不同的顺序显示,因此if 语句将在某些浏览器上触发,但在其他浏览器上不会触发。因此,当if 语句没有被无限循环执行时,系统将挂起。

【讨论】:

  • 天哪,谢谢!我不敢相信我错过了!!!我想是时候休息了……
  • 哈哈恰好发生在我们当中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-04
  • 2018-02-17
  • 1970-01-01
  • 2016-10-08
  • 1970-01-01
相关资源
最近更新 更多