IE 11上的MutationObserver语法错误

时间:2018-05-29 21:41:37

标签: javascript internet-explorer syntax-error mutation-observers

当我切换面板的内容时,我正在使用MutationObserver来更改某些变量的值(我正在使用Bootstrap选项卡)。在Chrome和Firefox中,一切都运行得很好,但出于某种原因,当我用IE测试它时,它在控制台中显示语法错误并且脚本中断。这是我的MutationObserver代码:

var observer = new MutationObserver(function (MutationRecords, MutationObserver) {
        dataTable = null;
        tabla = null;
        tabActiva = $('.tab-content').find('.active');
        formFiltro = tabActiva.find('form');
        tabla = tabActiva.find('table');
    });

    observer.observe(target, {
        childList: true,
        attributeFilter: ['class'],
        subtree: true
    });

控制台指出错误在observer.observe()上。我不知道发生了什么。提前谢谢。

enter image description here

以防万一,这是我的“目标”:

var target = $('.tab-content > .tab-pane').get(0);

2 个答案:

答案 0 :(得分:5)

使用MutationObserver,可以过滤属性,但前提是您要观察元素属性。因此,选项attributeFilter仅在attributes设置为true时适用。

如果您指定attributeFilter但未将attributes设置为true,则IE11将抛出语法错误,而Chrome和Firefox将默默忽略attributeFilter

要解决语法错误,请将attributes设置为true或删除attributeFilter

答案 1 :(得分:2)

  1. 根据MDN,如果指定了attributeFilter属性,则
  

没有必要,也暗示将attributes设置为true

  1. DOM Living Standard specificationattributeFilter定义为
  

如果不是全部,则设置为属性本地名称(无名称空间)的列表   需要观察属性突变,并且attributestrue或   省略


  • 看起来IE11并非如此-它不符合规范。
  • IE11的解决方法:同时设置attributeFilterattributes: true