Javascript 列出元素的所有绑定(使用 jQuery)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4138543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:03:06  来源:igfitidea点击:

List all bindings of an element (with jQuery)

javascriptjquerybinding

提问by Zardoz

Is there a way to list all bindings on a jQuery element? jQuery's bind() does only seem to attach them and I didn't find a jQuery function that does get the bindings.

有没有办法列出 jQuery 元素上的所有绑定?jQuery 的 bind() 似乎只是附加了它们,我没有找到可以获取绑定的 jQuery 函数。

采纳答案by jAndy

This answer applies to jQuery version < 1.8

此答案适用于 jQuery 版本 < 1.8

Best way to do that, probably the FireQueryplugin for FireFox. Really a neat tool.

最好的方法,可能是FireQueryFireFox的插件。真是一个整洁的工具。

If you want/need to accomplish that "in-code", use jQuerys .data('events')object.

如果您想要/需要完成“代码中”,请使用 jQuerys.data('events')对象。

$.each($('#element').data('events'), function(i, e) {
    console.log(i, e);
});

All events that were bound via jQuery gets pushed into that object. Of course, there might also be other event handlers like on-anything, for which you would have to check explicitly.

所有通过 jQuery 绑定的事件都会被推送到该对象中。当然,可能还有其他事件处理程序,如on-anything,您必须明确检查。

Ref.: FireQuery

参考:FireQuery

回答by Grinn

As of jQuery 1.8 the new syntax is:

从 jQuery 1.8 开始,新语法是:

$.each($._data("#element", "events"), function(i, e) {
console.log(i, e);
});

Note that it's $._data("#element"NOT$.data($("#element"), so be sure to unwrap your selector if you need to using $myJQuerySelector[0].

请注意,它$._data("#element"不是$.data($("#element"),因此如果您需要使用$myJQuerySelector[0].

Read more...

阅读更多...

回答by vaskin

There must be a way to do it programatically, and someone has figured it out and put it in a visual tool.

必须有一种方法可以通过编程来完成,并且有人已经弄清楚并将其放入可视化工具中。

I'm not sure if this answers your question, but I've found the best tool for determining these bindings is called Visual Event(not a great name, very hard to google actually).

我不确定这是否能回答您的问题,但我发现确定这些绑定的最佳工具称为Visual Event(不是一个好名字,实际上很难用谷歌搜索)。

This works in Firefox and Chrome, Chromium, Safari, etc. Some binding issues may happen differently in different browsers. It's good to cover all the bases.

这适用于 Firefox 和 Chrome、Chromium、Safari 等。一些绑定问题在不同的浏览器中可能会发生不同的情况。涵盖所有基础是很好的。

If you have an overlay and need to get underneath an element you can double-click any binding to hide it to get to the event you need to view.

如果您有一个叠加层并且需要进入某个元素下方,您可以双击任何绑定来隐藏它以到达您需要查看的事件。

alt text

替代文字

回答by Habeeb Perwad

Based on the other answers and comments.

基于其他答案和评论。

JQuery 1.8 and above

JQuery 1.8 及以上

var elementSelector = '#element-id';

$(elementSelector).bind('click.test', function(){}); // For testing.

$.each($._data($(elementSelector)[0], 'events'), function(i, e) {
    console.log(i, e);
});

The first argument of $._data()is not jquery object.

的第一个参数$._data()不是 jquery 对象。

Note that this is not a supported public interface; the actual data structures may change incompatibly from version to version. - Jquery 1.8 Realeased

请注意,这不是受支持的公共接口;实际的数据结构可能会因版本不同而发生不兼容的变化。- Jquery 1.8 Realeased



Below JQuery 1.8

低于 JQuery 1.8

var elementSelector = '#elementid';

$(elementSelector).bind('click.test', function(){}); // For testing.

$.each($(elementSelector).data('events'), function(i, e) {
    console.log(i, e);
});

In version 1.6, jQuery separated its internal data from the user's data to prevent name collisions. However, some people were using the internal undocumented “events” data structure so we made it possible to still retrieve that via .data(). This is removed in 1.8.- Jquery 1.8 Realeased

在 1.6 版本中,jQuery 将其内部数据与用户数据分开以防止名称冲突。然而,有些人正在使用内部未记录的“事件”数据结构,因此我们仍然可以通过 .data() 检索它。这在 1.8.- Jquery 1.8 Realeased 中被删除

回答by Lee

The following code looks at an element and all its children and console.logs any bound events.

以下代码查看一个元素及其所有子元素并 console.logs 任何绑定事件。

function findMyEvents(me) {

    if (typeof $._data($(me)[0], 'events') !== "undefined") {
        console.log($(me)[0])
        console.log($._data($(me)[0], 'events'))
        $._data($(me)[0], 'events')
    };
    for (var i = 0; i < $(me).children().length; i++) {
        findMyEvents($(me).children()[i])
    }
}

findMyEvents('body')

回答by Phong Le

I cannot make Grinn's answer work. From jQuery 1.8 find event handlers, I think the example should be changed to

我不能让格林的回答起作用。从jQuery 1.8 find event handlers,我认为这个例子应该改为

var obj = $('#element');
$.each($._data(obj[0], "events"), function(i, e) {
console.log(i, e);
});