admin管理员组文章数量:1133738
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.
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.
Share Improve this question edited Sep 6, 2019 at 18:37 Brian Tompsett - 汤莱恩 5,87572 gold badges61 silver badges133 bronze badges asked Sep 7, 2011 at 17:44 FMaz008FMaz008 11.3k19 gold badges70 silver badges105 bronze badges8 Answers
Reset to default 153Using 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.
You can also use Chrome's inspector to find attached events another way, as follows:
- Right click on the element to inspect, or find it in the 'Elements' pane.
- Then in the 'Event Listeners' tab/pane, expand the event (eg 'click')
- Expand the various sub-nodes to find the one you want, and then look for where the 'handler' sub-node is.
- Right click the word 'function', and then click 'Show function definition'
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
Give it a try to the jQuery Audit extension (https://chrome.google.com/webstore/detail/jquery-audit/dhhnpbajdcgdmbbcoakfhmfgmemlncjg), after installing follow these steps:
- Inspect the element
- On the new 'jQuery Audit' tab expand the Events property
- Choose for the Event you need
- From the handler property, right click over function and select 'Show function definition'
- You will now see the Event binding code
- Click on the 'Pretty print' button for a more readable view of the code
(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 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)
getEventListeners($0)
Whereas $0 is the selected element in the DOM.
https://developers.google.com/web/tools/chrome-devtools/console/command-line-reference#0_-_4
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)
- right click anywhere on a page (in chrome)
- click 'Inspect Element'
- click the 'Scripts' tab
- Search bar in the top right
Doing a quick search for the #ID should take you to the binding function eventually.
Ex: searching for #foo
would take you to
$('#foo').click(function(){ alert('bar'); })
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:
findEventHandlers("click", "#clickThis");
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/
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.
本文标签: javascriptUsing Chromehow to find to which events are bound to an elementStack Overflow
版权声明:本文标题:javascript - Using Chrome, how to find to which events are bound to an element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736785386a1952829.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论