如何找出对象的 onclick 事件正在调用什么 javascript 函数?

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

How do I find out what javascript function is being called by an object's onclick event?

javascript

提问by Cyrcle

How do I find out what javascript function is being called by an object's onclick event? Even better, can I then find out which included .js file that function is in?

如何找出对象的 onclick 事件正在调用什么 javascript 函数?更好的是,我可以找出该函数在哪个包含的 .js 文件中吗?

采纳答案by Adil

You can do like this

你可以这样做

With Javascript Demo on JsFiddle

使用JsFiddle 上的Javascript演示

div1 = document.getElementById('div1');

alert(div1.getAttribute("onclick"));?

With jQuery Demo on JsFiddle

使用JsFiddle 上的jQuery演示

<div id="div1" onclick="myfun();" >?

alert($('#div1').attr('onclick'))?;

回答by benekastah

I use Chrome's Developer Tools for this:

我为此使用 Chrome 的开发人员工具:

Event Listener Breakpoints in Google Chrome's developer tools

Event Listener Breakpoints in Google Chrome's developer tools

Check the clickbox, and then click on the element on the page you want to find the handler for. If you are using jQuery (or similar library), you may have to step through their code before you get to yours.

选中该click框,然后单击页面上要为其查找处理程序的元素。如果您正在使用 jQuery(或类似的库),您可能必须在进入您的代码之前单步执行他们的代码。

回答by Elliot Bonneville

You wouldn't be able to find out the file the onclick event is called from but myObject.onclickwill give you the function that's being called. And no, you don't need jQuery for this.

您将无法找到调用 onclick 事件的文件,但myObject.onclick会为您提供正在调用的函数。不,你不需要jQuery。

As far as getting the nameof the function, that's a little more complicated. You could try something like this, perhaps:

至于获取函数的名称,那就有点复杂了。你可以尝试这样的事情,也许:

var myFunc = myObject.onclick, myFuncName = "";

for(prop in window) {
    if(window.hasOwnProperty(prop) && window[prop] === myFunc) {
        myFuncName = prop; // myFuncName is now the name of the function. This only works if you didn't assign an anonymous function to the click handler.
        break;
    }
}

But honestly, I think that's a little overkill.

但老实说,我认为这有点矫枉过正。

回答by Gabe

That depends on how the eventis attached.

这取决于如何event附加。

If you're binding to onclickwithout something like jQuery you could do this:

如果你绑定到onclick没有像 jQuery 这样的东西,你可以这样做:

var obj = document.getElementById('elementId');
console.log(obj.onclick);

回答by Widor

I do this using this Visual Eventscript which neatly highlights which events are subscribed by which functions on which elements.

我使用这个Visual Event脚本来做到这一点,它巧妙地突出显示了哪些事件由哪些元素上的哪些函数订阅。

To find the souce of the code, simply use FireBug or similar browser developer tools to search the function name.

要查找代码的来源,只需使用 FireBug 或类似的浏览器开发人员工具来搜索函数名称。

回答by Alper Ebicoglu

I have a different approach. I overload onclick function and add my debugger before the real function.

我有不同的方法。我重载 onclick 函数并在真正的函数之前添加我的调试器。

This is the element

这是元素

<div id="div1">?

Write this JavaScript to developer console

将此 JavaScript 写入开发者控制台

var clickFn = $("#div1").click; 

$("#div1").click(function(){ 
    debugger; 
    clickFn(); 
});