简体   繁体   English

如何找到jQuery事件的原始(匿名函数)源?

[英]How to find the original (Anonymous function) source of jQuery events?

Most jQuery code uses anonymous functions, such as: 大多数jQuery代码使用匿名函数,例如:

jQuery('someelements').someEvent(function() {
    // code here
});

This works well, but doesn't do so well for debugging. 这很好用,但是在调试方面做得不好。 I tried to find the source of some anonymous functions using both Firefox Firebug and Chrome's inspector, with the pause javascript functionality, but the actual code it calls is in the jQuery js file, and stepping through the code never tells what line, or even what .js file added that event. 我试图使用Firefox Firebug和Chrome的检查器同时具有暂停javascript功能来查找一些匿名函数的来源,但是它调用的实际代码在jQuery js文件中,并且单步执行该代码永远不会告诉哪一行,甚至什么.js文件添加了该事件。 How can I see where the action is defined? 如何查看动作的定义位置?

There's a Google Chrome/Firefox plugin that can allow you to see events as registered by jQuery or other (particular) libraries: Visual Event . 有一个Google Chrome / Firefox插件,可让您查看jQuery或其他(特定)库注册的事件Visual Event

Similar question: Firefox extension to find out which Javascript event is bound to an inspected element? 类似的问题: Firefox扩展找出哪个Javascript事件绑定到受检查的元素?

Try using the non-minified version of jQuery and using the profile functionality in Firebug to find the exact line in the jQuery source that is being called on the occurance of an event in your code. 尝试使用非缩小版本的jQuery并使用Firebug中的profile功能在代码中发生事件时在jQuery源中找到要调用的确切行。

If your intention is to find the implementation of some jQuery selectors or functions implementation, please refer this amazing resource that does exactly that: 如果您打算查找某些jQuery选择器的实现或函数的实现,请参考此功能惊人的资源,它确实做到了:
http://www.keyframesandcode.com/resources/javascript/deconstructed/jquery/ http://www.keyframesandcode.com/resources/javascript/deconstructed/jquery/

I'm not sure I'm fully in the context. 我不确定我是否完全了解上下文。 But if you find the function as a value (for example among some object properties), you always can navigate to its source from the context menu in Chrome DevTools. 但是,如果您将函数作为值(例如,在某些对象属性中)找到,则始终可以从Chrome DevTools的上下文菜单中导航至其源。

One thing I've found helpful is to give the event handler a name despite it not being called anywhere else. 我发现有帮助的一件事是为事件处理程序命名,尽管该名称未在其他任何地方被调用。 That way it's easier to identify it in profiling tools. 这样,在分析工具中更容易识别它。

eg 例如

jQuery('someelements').someEvent(function someEventHandler() {
    // code here
});

Once event handlers all have names you can hack the jquery dev version to log the event and handler names. 一旦事件处理程序都具有名称,您就可以破解jquery dev版本以记录事件和处理程序名称。 I add this in the event dispatch method just above ret = ... .apply() 我在ret = ... .apply()上方的事件分派方法中添加此ret = ... .apply()

var handler = ( (jQuery.event.special[ handleObj.origType ] || {}).handle || handleObj.handler );
console.log("FIRE EVENT ", event.type, "HANDLER: ", handler.name||'Anonymous Function'); 
jQuery('someelements').someEvent(function(e) {
    // code here
alert(e.target);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM