确定事件处理程序在DOM元素上消失的原因

时间:2022-09-19 00:01:57

(In Chrome) Is there a simple way to determine when an event handler (for click) is removed from an element? An event handler of mine is mysteriously disappearing, but I'm not sure if there's a quick and easy way to either pause or get a stack trace/exception when that occurs.

(在Chrome中)是否有一种简单的方法可以确定何时从元素中删除事件处理程序(用于单击)?我的事件处理程序神秘地消失了,但是我不确定是否有一种快速简便的方法可以暂停或获得堆栈跟踪/异常。

1 个解决方案

#1


6  

In your debug environment, run this code and look at your console.

在调试环境中,运行此代码并查看控制台。

(function () {
    var ael = Node.prototype.addEventListener,
        rel = Node.prototype.removeEventListener;
    Node.prototype.addEventListener = function (a, b, c) {
        console.log('Listener', 'added', this, a, b, c);
        ael.apply(this, arguments);
    };
    Node.prototype.removeEventListener = function (a, b, c) {
        console.log('Listener', 'removed', this, a, b, c);
        rel.apply(this, arguments);
    };
}());

If it is necessary to see more information, you may also want to call console.trace or give the functions a name so you can use .caller (arguments.callee is depreciated, hence using a name to get reference to self)

如果有必要查看更多信息,您可能还需要调用console.trace或给函数命名,以便可以使用.caller(arguments.callee被折旧,因此使用名称来引用self)

#1


6  

In your debug environment, run this code and look at your console.

在调试环境中,运行此代码并查看控制台。

(function () {
    var ael = Node.prototype.addEventListener,
        rel = Node.prototype.removeEventListener;
    Node.prototype.addEventListener = function (a, b, c) {
        console.log('Listener', 'added', this, a, b, c);
        ael.apply(this, arguments);
    };
    Node.prototype.removeEventListener = function (a, b, c) {
        console.log('Listener', 'removed', this, a, b, c);
        rel.apply(this, arguments);
    };
}());

If it is necessary to see more information, you may also want to call console.trace or give the functions a name so you can use .caller (arguments.callee is depreciated, hence using a name to get reference to self)

如果有必要查看更多信息,您可能还需要调用console.trace或给函数命名,以便可以使用.caller(arguments.callee被折旧,因此使用名称来引用self)