列出jQuery中的所有直播活动

Mar*_*rio 8 javascript jquery

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

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

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

livefor 绑定的函数对应的数组的索引是什么$('#certain_element')

Mar*_*rio 3

好吧,想通了。对于click事件, for $('#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));
});
Run Code Online (Sandbox Code Playgroud)