javascript jQuery绑定对象上的所有事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6577346/
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-10-25 21:09:42  来源:igfitidea点击:

jQuery bind all events on object

javascriptjquery

提问by Harry

Possible Duplicate:
How all events of dom element can be bind?

可能的重复:
如何绑定 dom 元素的所有事件?

I have an object that gets events triggered against it.

我有一个对象可以触发针对它的事件。

How do I bind all the events and console.log the event name?

如何绑定所有事件和 console.log 事件名称?

回答by Kevin Stricker

You can bind all the standard javascript events by including them in the bind call separated by spaces. The jQuery docsprovide this list:

您可以通过将所有标准 javascript 事件包含在以空格分隔的绑定调用中来绑定它们。 jQuery 文档提供了这个列表:

blur focus focusin focusout load resize scroll unload click dblclick mousedown mouseup  mousemove mouseover mouseout mouseenter mouseleave change select submit keydown keypress keyup error

So, to bind the event for click, blur and focus:

因此,要绑定单击、模糊和焦点事件:

   $('#foo').bind('click blur focus', function(event) {
      console.log(event.type);
    });

If you're looking for custom events, you'll have to look at the API and bind those as well. There doesn't appearto be any way to bind on anyevent.

如果您正在寻找自定义事件,则必须查看 API 并绑定它们。虽然目前没有出现是对绑定任何方式任何事件。