javascript 如何找到附加到按钮点击事件的方法

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

How to find the method that is attached to a click event of a button

javascriptjqueryfirebug

提问by themhz

How can I find out from firebug which method or methods are attached to the click event handler of a button?

如何从萤火虫中找出哪些方法或方法附加到按钮的单击事件处理程序?

This is the code of the button

这是按钮的代码

<button class="rsmg_button_save ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary ui-state-focus" type="button" role="button" aria-disabled="false" style="display: inline-block;"><span class="ui-button-icon-primary ui-icon ui-icon-disk"></span><span class="ui-button-text">Save</span></button>

回答by xyz

If function is attached using jQuery, you can see it using firebug.
jQuery events are stored using datamethod and with key as events.
So $("desiredSelector").data("events")will show up all events attached to that particular element.
For details check this link.
For event defined in Javascript you can check the onClickproperty or use the method suggested by jfriend00.

如果使用 jQuery 附加函数,您可以使用 firebug 看到它。
jQuery 事件使用datamethod 和 key as存储events
So$("desiredSelector").data("events")将显示附加到该特定元素的所有事件。
有关详细信息,请查看此链接
对于 Javascript 中定义的事件,您可以检查onClick属性或使用 jfriend00 建议的方法。



EDIT :
There is a jQuery pluginwhich make this more simple.

编辑:
有一个jQuery 插件,使这更简单。

回答by jfriend00

Javascript code can hook up to button click events with either the onclickproperty or with addEventListener()or attachEvent()(depending upon browser and browser version).

Javascript 代码可以使用onclick属性或使用addEventListener()attachEvent()(取决于浏览器和浏览器版本)连接到按钮单击事件。

If there is javascript code handling the event and there is no onclick handler specified in the HTML, then the javascript code is installing the event handler either by setting the onclickproperty later or by using addEventListener()or attachEvent().

如果有处理事件的 javascript 代码并且 HTML 中没有指定 onclick 处理程序,则 javascript 代码通过onclick稍后设置属性或使用addEventListener()或来安装事件处理程序attachEvent()

回答by Friedrich K?lbel

I searched hard with $("desiredSelector").data("events") from above and also with GetEventListenersbut in Firebug 2.x there is a very easy new way to find the bound events: If there is a bind-function you will find an "ev" right next to the row:

我从上面用 $("desiredSelector").data("events") 和GetEventListeners 努力搜索,但在 Firebug 2.x 中,有一种非常简单的新方法可以找到绑定事件:如果有一个绑定函数,你会在该行旁边找到一个“ev”:

enter image description here

在此处输入图片说明

Cool Thing!

很酷的东西!