如何调试 JAVASCRIPT 事件?或者如何让所有函数都调用trace?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3509032/
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
How to debug JAVASCRIPT events? Or how to make all functions call trace?
提问by gennad
For example there is a button. It is wrapped by <div>.
例如有一个按钮。它由<div>.
When pressing to this button, there is javascript function call happen, then another function, then calling by ajax to the server and if it's OK, javascript redirecting this page to another page.
当按下这个按钮时,会发生 javascript 函数调用,然后是另一个函数,然后通过 ajax 调用服务器,如果没问题,javascript 将此页面重定向到另一个页面。
It's hard to debug.
很难调试。
Is it possible to "catch" this event? I.e. to know, what function is called after the click on the button? Button doesn't have attribute "onclick" i.e. event listener is connected in javascript.
是否有可能“抓住”这个事件?即要知道,点击按钮后调用了什么函数?按钮没有属性“onclick”,即事件侦听器在 javascript 中连接。
And if it's not possible then is it possible to make trace? That is to look at all functions calls, which is called after which?
如果不可能,那么是否可以进行跟踪?那就是看所有的函数调用,哪个是在哪个之后调用的?
It would be better in visual way, though in textual is also good:)
视觉上会更好,但文字上也不错:)
Thanks for replies!
感谢您的回复!
回答by desau
Yeah - this sort of thing is not as simple is you would like.
是的 - 这种事情并不像你想要的那么简单。
Google Chrome has an Event Listenerspanel. Right-click your button, then select Inspect Element. Make sure the correct element is selected, then check the Event Listenerspanel on the right.
谷歌浏览器有一个事件监听器面板。右键单击您的按钮,然后选择Inspect Element。确保选择了正确的元素,然后检查右侧的Event Listeners面板。
You can also use the debuggerkeyword to set a breakpoint in the call stack somewhere. Then use your favorite javascript debugger (built-in dev tools in Safari, Google Chrome & IE8, firebug for Firefox). In each of these, there's a call stack that'll allow you to navigate through the current call stack.
您还可以使用debugger关键字在调用堆栈中的某处设置断点。然后使用您最喜欢的 javascript 调试器(Safari、Google Chrome 和 IE8 中的内置开发工具,Firefox 的 firebug)。在其中的每一个中,都有一个调用堆栈,可让您浏览当前的调用堆栈。

