2011-09-20 41 views
0

给定Dom中的元素,是否可以返回特定事件的事件处理函数的名称?返回元素的特定事件处理函数

例如,如果我选择一个元素

$("#container") 

是否有可能返回绑定到该元素的“按键”事件处理程序?

+0

什么是最终的意向? – Saket

+0

我需要在页面中获取我正在处理的浏览器扩展的事件处理程序。 – Scaraffe

+0

jQuery只会知道我们使用jQuery创建的事件处理程序。 – Anurag

回答

0

你可以用得到它:$('#container').attr('keypress')

0

技术上是可以的,如果你使用一个命名函数(虽然这是一种奇怪的事情做在首位...)。

// create a named function 
function fooHandler() {} 

// bind the named function 
$("#container").keypress(fooHandler); 

// retrieve the bound function from the data collection 
$("#container").data('events').keypress[0].handler.name; // 'fooHandler' 
+0

我认为这只适用于使用jquery创建的事件。我正试图从已经从浏览器扩展中加载的页面获取事件。 – Scaraffe

1

这篇文章可能会有帮助:http://james.padolsey.com/javascript/things-you-may-not-know-about-jquery/

您可以通过jQuery的事件存储访问绑定到一个元素的所有事件处理程序(或任何对象):

// List bound events: 
console.dir(jQuery('#elem').data('events')); 

// Log ALL handlers for ALL events: 
jQuery.each($('#elem').data('events'), function(i, event){ 
    jQuery.each(event, function(i, handler){ 
     console.log(handler.toString()); 
    }); 
}); 

// You can see the actual functions which will occur 
// on certain events; great for debugging! 
+0

你可以做一个演示吗?它不适合我 - http://jsfiddle.net/yKBSz/ –

+0

@ŠimeVidas在这里你去:http://jsfiddle.net/yKBSz/1/ –

+0

我只注意到,“jQuery(边缘)”使用最新的alpha版本的jQuery哈哈......我在演示中使用这个选项已经有一段时间了。 –