代码之家  ›  专栏  ›  技术社区  ›  Keith Bentrup

更新:如何在prototype中的DOM节点上查找事件侦听器?

  •  6
  • Keith Bentrup  · 技术社区  · 15 年前

    this question .

    在Prototype 1.6+中,似乎不再使用Event.observators(可能是为了避免内存泄漏),那么现在我如何跟踪将哪些事件侦听器附加到元素?

    2 回复  |  直到 7 年前
        1
  •  7
  •   kangax    15 年前

    现在通过元素存储进行路由:)

    Element.getStorage(yourElement).get('prototype_event_registry') 将给你一个原型的例子 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.
    

    这些是未记录的内部详细信息 这在将来可能会改变,所以我不会依赖它们,除了调试目的。

        2
  •  7
  •   Community CDub    7 年前

    you linked to 更全面 Prototype 考虑版本变更的覆盖率 1.6.0 1.6.1 .

    这两者之间非常混乱,但1.6.1有点干净:

    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!') }"
    })