Update: How to find event listeners on a DOM node in prototype?

淺唱寂寞╮ 提交于 2019-11-28 01:12:11
Crescent Fresh

I've update the answer you linked to with more comprehensive Prototype coverage accounting for changes in versions 1.6.0 to 1.6.1.

It got very messy in between there, but 1.6.1 is somewhat clean:

var handler = function() { alert('clicked!') };
$(element).observe('click', handler);

// inspect
var clickEvents = element.getStorage().get('prototype_event_registry').get('click');
clickEvents.each(function(wrapper){
    alert(wrapper.handler) // alerts "function() { alert('clicked!') }"
})

Things are now routed through Element storage : )

Element.getStorage(yourElement).get('prototype_event_registry') will give you an instance of Prototype's Hash, so you can do anything that you would do with hash.

// to see which event types are being observed
Element.getStorage(yourElement).get('prototype_event_registry').keys();

// to get array of handlers for particular event type
Element.getStorage(yourElement).get('prototype_event_registry').get('click');

// to get array of all handlers
Element.getStorage(yourElement).get('prototype_event_registry').values();

// etc.

Note that these are undocumented internal details which might be changed in the future, so I wouldn't rely on them except for, perhaps, debugging purposes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!