简体   繁体   English

removeEventListener在IE10中不起作用,在Chrome中工作

[英]removeEventListener not working in IE10, works in Chrome

I am trying to create a mini library using MutationObserver to detect changes in DOM but also to fallback to basic Mutation Events in older browsers. 我正在尝试使用MutationObserver创建一个迷你库,以检测DOM中的更改,还可以回退到旧版浏览器中的基本Mutation事件。 So far so good, it works properly in Chrome and Firefox and I tested the fallback in these browsers too - everything worked but when I tested it IE 10 it behaves differently (how unexpected...) 到目前为止,它很好,它可以在Chrome和Firefox中正常工作,我也测试了这些浏览器的后备功能-一切正常,但是当我测试IE 10时,它的表现却有所不同(出乎意料的...)

In this library you can simply call: 在此库中,您可以简单地调用:

domWatch(someDOMNode, callback)

callback gets one parameter - an observer with disconnect & observe methods so you can stop watching the node while you do some changes to it and then start watching again. 回调得到一个参数-具有断开连接和观察方法的观察者,因此您可以在对节点进行一些更改时停止观察该节点,然后再次开始观察。

domWatch(document.querySelector('div'), function (obs) {
    setTimeout(function () {
        obs.disconnect();
        $(obs.node).append('a');
        obs.observe();
    }, 1200);
});

In the fallback I use mutation events and add/remove event listeners. 在后备中,我使用突变事件和添加/删除事件侦听器。 I think the problem is that in IE the event is not really removed is, it gets stuck in infinite loop (my code is in setTimeout so it will not crash your browser). 我认为问题在于,在IE中并未真正删除该事件,它陷入了无限循环(我的代码位于setTimeout中,因此不会导致浏览器崩溃)。 For some reason it probably thinks that this.realCallback are different functions? 由于某种原因,它可能认为this.realCallback是不同的函数?

FallbackObserver.prototype.observe = function () {
    this.node.addEventListener("DOMSubtreeModified", this.realCallback, true);
}

FallbackObserver.prototype.disconnect = function () {
    this.node.removeEventListener("DOMSubtreeModified", this.realCallback, true);
}

I created a mini fiddle only with the problematic code, try to run it in Chrome and then in IE10: http://jsfiddle.net/Hb45w/2/ 我只用有问题的代码创建了一个小提琴,尝试在Chrome中运行它,然后在IE10中运行: http : //jsfiddle.net/Hb45w/2/

(Full library Fiddle: http://jsfiddle.net/x6W3p/ ) (完整的库Fiddle: http : //jsfiddle.net/x6W3p/

Thanks for any help! 谢谢你的帮助!

Managed to solve it: 设法解决它:

All browsers were correctly removing and adding events - the problem was something else. 所有的浏览器都正确地删除和添加了事件-问题出在其他方面。

The actual problem with IE is probably some aspect of rendering engine - I disconnected the event handler, did the change and started observing again: IE的实际问题可能是呈现引擎的某些方面-我断开了事件处理程序的连接,进行了更改并开始再次观察:

    obs.disconnect();
    $(obs.node).append('a');
    obs.observe();

The problem was that the event was obviously attached back before IE finished the rendering (probably some asynchronous execution). 问题在于该事件显然是 IE完成渲染之前附加的(可能是一些异步执行)。

Changing 改变中

FallbackObserver.prototype.observe = function () {
    this.node.addEventListener("DOMSubtreeModified", this.realCallback, true);
}

to

FallbackObserver.prototype.observe = function () {
    var that = this;
    setTimeout(function () {
       that.node.addEventListener("DOMSubtreeModified", that.realCallback, true);
    }, 0);
}

did the trick. 做到了。 But I am not sure whether it will work if some more complex DOM changes will be performed. 但是我不确定如果执行一些更复杂的DOM更改是否会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM