使用 Chrome,如何查找哪些事件绑定到元素
假设我的页面上有一个链接:
<a href="#" id="foo">Click Here</a>
我不知道其他任何内容,但是当我单击该链接时,会显示一个 alert("bar")
。 所以我知道在某个地方,某些代码被绑定到 #foo
。
如何找到将 alert("bar")
绑定到点击事件的代码? 我正在寻找 Chrome 的解决方案。
Ps.:这个例子是虚构的,所以我不是在寻找像这样的解决方案:“使用 XXXXXX 并在整个项目中搜索“alert(\”bar\”)”。我想要一个真正的调试/跟踪解决方案。
Lets suppose I've a link on my page:
<a href="#" id="foo">Click Here</a>
I don't know anything else, but when I click on the link, an alert("bar")
is displayed.
So I know that somewhere, some code is getting bound to #foo
.
How can I find the code that is binding the alert("bar")
to the click event?
I'm looking for a solution with Chrome.
Ps.: The example is fictive, so I'm not looking for solution like: "Use XXXXXX and search the whole project for "alert(\"bar\")". I want a real debugging/tracing solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用 Chrome 15.0.865.0 开发版。元素面板上有一个“事件监听器”部分:
脚本上有一个“事件监听器断点”控制板。使用鼠标 ->单击断点,然后“单步执行下一个函数调用”,同时密切关注调用堆栈以查看哪个用户态函数处理该事件。理想情况下,您可以将 jQuery 的缩小版替换为未缩小版,这样您就不必一直介入,并在可能的情况下使用逐步跨越。
Using Chrome 15.0.865.0 dev. There's an "Event Listeners" section on the Elements panel:
And an "Event Listeners Breakpoints" on the Scripts panel. Use a Mouse -> click breakpoint and then "step into next function call" while keeping an eye on the call stack to see what userland function handles the event. Ideally, you'd replace the minified version of jQuery with an unminified one so that you don't have to step in all the time, and use step over when possible.
您还可以使用 Chrome 的检查器以另一种方式查找附加事件,如下所示:
这将带您到定义处理程序的位置,如下图所示,Paul Irish 在此进行了解释:https://groups.google.com/forum/#!topic/google-chrome- developer-tools/NTcIS15uigA
You can also use Chrome's inspector to find attached events another way, as follows:
This will take you to where the handler was defined, as demonstrated in the following image, and explained by Paul Irish here: https://groups.google.com/forum/#!topic/google-chrome-developer-tools/NTcIS15uigA
尝试一下 jQuery Audit 扩展 (https://chrome.google.com/webstore/detail/ jquery-audit/dhhnpbajdcgdmbbcoakfhmfgmemlncjg),安装后按照以下步骤操作:
Give it a try to the jQuery Audit extension (https://chrome.google.com/webstore/detail/jquery-audit/dhhnpbajdcgdmbbcoakfhmfgmemlncjg), after installing follow these steps:
(最新截至 2022 年)对于 Chrome 版本 99 版:
选择要检查的元素
选择“事件侦听器”选项卡
确保选中“框架侦听器”以显示真正的 javascript 文件而不是 jquery 函数。
(Latest as of 2022) For version Chrome Version Version 99:
Select the element you want to inspect
Choose the Event Listeners tab
Make sure to check the Framework listeners to show the real javascript file instead of the jquery function.
2018 更新 - 可能对未来的读者有所帮助:
我不确定 Chrome 最初何时引入此功能。但现在可以在 Chrome 中完成此操作的另一种(简单)方法是通过控制台命令。
例如:(在 Chrome 控制台中输入)
而 $0 是 DOM 中选定的元素。
https://developers.google.com /web/tools/chrome-devtools/console/command-line-reference#0_-_4
2018 Update - Might be helpful for future readers:
I am not sure when this was originally introduced in Chrome. But another (easy) way this can be done now in Chrome is via console commands.
For example: (in chrome console type)
Whereas $0 is the selected element in the DOM.
https://developers.google.com/web/tools/chrome-devtools/console/command-line-reference#0_-_4
编辑:代替我自己的答案,这个答案相当出色:如何使用 Firebug(或类似工具)调试 JavaScript/jQuery 事件绑定
Google Chrome 开发者工具内置搜索功能脚本部分
如果您不熟悉此工具:(以防万一)
快速搜索 # ID 最终会带您进入绑定功能。
例如:搜索
#foo
会将您带到Edit: in lieu of my own answer, this one is quite excellent: How to debug JavaScript/jQuery event bindings with Firebug (or similar tool)
Google Chromes developer tools has a search function built into the scripts section
If you are unfamiliar with this tool: (just in case)
Doing a quick search for the #ID should take you to the binding function eventually.
Ex: searching for
#foo
would take you tofindEventHandlers 是一个 jquery 插件,原始代码在这里: https://raw.githubusercontent .com/ruidfigueiredo/findHandlersJS/master/findEventHandlers.js
步骤
将原始代码直接粘贴到chrome 的控制台(注意:必须已经加载 jquery)
使用以下函数调用:
findEventHandlers(eventType, 选择器) ;
查找对应的选择器指定元素的 eventType 处理程序。
示例:
如果有的话,可用的事件处理程序将显示在下面,您需要展开以找到处理程序,右键单击该函数并选择
显示函数定义
参见:https://blinkingcaret.wordpress.com/2014/01/17/quickly-finding-and-debugging-jquery-event-handlers/
findEventHandlers is a jquery plugin, the raw code is here: https://raw.githubusercontent.com/ruidfigueiredo/findHandlersJS/master/findEventHandlers.js
Steps
Paste the raw code directely into chrome's console(note:must have jquery loaded already)
Use the following function call:
findEventHandlers(eventType, selector);
to find the corresponding's selector specified element's eventType handler.
Example:
Then if any, the available event handler will show bellow, you need to expand to find the handler, right click the function and select
show function definition
See: https://blinkingcaret.wordpress.com/2014/01/17/quickly-finding-and-debugging-jquery-event-handlers/
对于 Chrome 版本 52.0.2743.116:
在 Chrome 的开发者工具中,按
Ctrl
+Shift
+F
打开“搜索”面板。输入您要查找的元素的名称。
绑定元素的结果应出现在面板中并说明它们所在的文件。
For Chrome Version 52.0.2743.116:
In Chrome's Developer Tools, bring up the 'Search' panel by hitting
Ctrl
+Shift
+F
.Type in the name of the element you're trying to find.
Results for binded elements should appear in the panel and state the file they're located in.