javascript - Mutation Observer 无法在受支持的 Microsoft Edge 版本上运行

标签 javascript html css browser microsoft-edge

我有以下使用mutationobserver的代码,它可以在浏览器chrome、firefox、opera中运行,但不能在浏览器边缘运行。

  var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
  var observer = new MutationObserver(function(mutations) {

   mutations.forEach(function(mutation) {

     if (mutation.type === 'childList') {
     if(mutation.target.innerHTML == 'Online - Chat Us'){
       //if button text changes from "Offline" to "Online - Chat Us"
       if(mutation.removedNodes[0].nodeValue === 'Offline' && mutation.addedNodes[0].nodeValue === 'Online - Chat Us'){
         //change top button to "Online - Chat Us" && false the disabled attribute.
         document.getElementById('mySecondButton').innerHTML = mutation.addedNodes[0].nodeValue;
         document.getElementById('mySecondButton').disabled = false;
     }
 }
     }else if(mutation.type === 'characterData'){
     //console.log(mutation);      
     }
   });
 });

仅在调试器的边缘浏览器中,我收到以下错误:SCRIPT5007:SCRIPT5007:无法获取未定义或空引用的属性“nodeValue”,它引用了这行代码if(mutation.target.innerHTML === '立即聊天'){

从下面的图表中我可以看到我使用的是受支持的版本。

Link to chart resource enter image description here

Edge浏览器版本截图。

enter image description here

最佳答案

在 Edge 中,innerHTML、innerText 和 textContent 更改导致的不是一条而是两条突变记录:一条用于删除旧节点,一条用于插入新节点。所有其他浏览器只有一个突变记录,其中包含一个已删除的节点和一个已插入的节点。我向脚本提供了一些空检查,以将突变记录缩小到只有一个,一切顺利。

关于javascript - Mutation Observer 无法在受支持的 Microsoft Edge 版本上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60622689/

相关文章:

javascript - 将 Kendo React 包含在脚本标签中

html - 跨度标记文本溢出 :ellipsis without using width

html - href 链接颜色已全局化,如何更改特定链接的颜色?

javascript - 如何在其中一个上使用溢出来放置相同高度的列?

javascript - 使用 jquery 1.7 同步 POST

javascript - javascript中输出变量值

javascript - 从 div 元素获取文件值

javascript - Window.close() 不适用于关闭当前选项卡

html - CSS不继承父类属性

python - 尝试使用 Scrapy 抓取表格