为什么在 IE < 9 中分离事件不会触发?



我已经设置了最简单的事件处理程序函数。一切都很好,除了IE中的events.remove();9. 所以,换句话说,除了在使用.detachEvent()的IE版本中删除事件之外,一切都工作得很好。

我一直在IETester和VirtualBox上测试,运行Windows XP和IE8。

events = {
    add: (function () {
        if (document.documentElement.addEventListener) {
            return function (elm, type, func) {
                elm.addEventListener(type, func, false);
            };
        } else if (document.documentElement.attachEvent) {
            return function (elm, type, func) {
                elm.attachEvent('on' + type, function () {
                    func.call(elm, window.event);
                });
                elm = null; //clean up possible memory leaks?
            };
        }
    }()),
    remove: (function () {
        if (document.documentElement.removeEventListener) {
            return function (elm, type, func) {
                elm.removeEventListener(type, func, false);
            };
        } else if (document.documentElement.detachEvent) {
            return function (elm, type, func) {
                elm.detachEvent('on' + type, function () {
                    func.call(elm, window.event);
                });
                elm = null; //clean up possible memory leaks?
            };
        }
    }())
};

这是一个(奇怪的)小提琴

因为您试图分离与绑定的函数不同的函数。

它们都是匿名函数这一事实并不重要。它们是两个不同的函数对象

最新更新