gpt4 book ai didi

javascript - 监听新注册的事件处理程序

转载 作者:数据小太阳 更新时间:2023-10-29 06:03:13 24 4
gpt4 key购买 nike

我目前正在为我的大学做一个项目。我需要做的一件事是将所有已注册的 JavaScript 事件处理程序与服务器同步。 IE。我需要知道哪些元素具有特定的事件处理程序。

我已经在使用 VisualEvent找出哪些元素具有事件处理程序,它的效果非常好。

但我需要的是有一个事件监听器,每次为 DOM 元素注册事件处理程序时都会调用它。

所以基本上每次都是 $("#foo").click(...)$("#foo").bind(...) 被调用,我需要获取已为此元素注册了新事件处理程序的信息。

反之亦然,当从 DOM 元素中删除事件处理程序时,我需要一个监听器,但这对于第一个原型(prototype)不是强制性的。

有没有一种方法可以将处理程序全局附加到所有事件处理程序注册?

如果您需要更多信息,请随时发表评论。

最佳答案

如果您使用的是 jQuery 1.7+,所有附加事件的方法都会通过 jQuery.fn.on,因此这是一个简单的覆盖该函数并变得疯狂的情况;

(function () {

var old = jQuery.fn.on;

jQuery.fn.on = function (events, selector, data, handler) {
// Ensure you still attach the events
var result = old.apply(this, arguments);

// Now do your own thing

// Inside here, `this` refers to the jQuery object on which `on` was invoked;
// it's not a specific element like it normally is within jQuery. You then
// therefore use something like `this.each(function () { /* this */ }); to
// target each element in the set.

// You might want to normalize the variables, as selector and data are optional,
// and events can be an object or string
jQuery.post('/spy.php', {
events: events,
selector: selector,
data: data
}, jQuery.noop);

return result; // keep the signature of `on`, and return the value `on()` *would* have done.
};

}());

如果您使用的是 jQuery < 1.7 且无法升级,您可以执行与上述类似的操作,但必须覆盖 bind()live(), delegate()

关于javascript - 监听新注册的事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9177364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com