列出jQuery中的所有直播活动

我怎样才能在jQuery中找到哪些事件与特定元素的实时绑定?

假设我有一个函数randomFunction ,它从函数数组中返回一个随机函数。 如何找到哪个函数绑定到某个元素?

 var arrayOfFunctions = []; //a whole bunch of functions function randomFunction(array){}; //returns one of those functions $('#certain_element').live('click', randomFunction(arrayOfFunctions)); 

对于$('#certain_element') live绑定的函数,数组的索引是什么?

好吧,想通了。 对于click事件,对于$('#certain_element') ,将每个绑定的索引记录到控制台:

 var relevantHandlers = $.map($(document).data('events').live, function(value){ if(value.origType == 'click' && value.selector == '#certain_element'){ return value.handler; } }; //all handlers for #certain_element bound to click by live. $.each(relevantHandlers, function(){ console.log("the index is: " + $.inArray(this, arrayOfFunctions)); }); 

看看这个插件 。 当我上次使用它时,需要稍微修改它为当时最新版本的jQuery,但它应该给你一个方向。

有一个叫做Visual Event的漂亮书签,它显示了将被调用的代码。

但是,因为你真的在调用一个随机函数,所以如果只是测试,可能会做一些简单的事情,如包含警报(“函数名称”)或colsone.log(“函数”)。