简体   繁体   English

attachEvent 或 addEventListener - 存储在哪里?

[英]attachEvent or addEventListener - stored where?

In jQuery, if I do this...在 jQuery 中,如果我这样做...

$('a').click(function(){
    // Do something 
});

...the click event is stored at $('a').data('events') and I can fetch it like so: ...点击事件存储在$('a').data('events')中,我可以像这样获取它:

jQuery.each($('a').data('events'), function(i, event){  
    jQuery.each(event, function(i, handler){
        if(handler.type.toString() == 'click')
        {
            // do something
        }
    });
});

An event that is attached via attachEvent or addEventListener will obviously not appear in $('a').data('events') .通过attachEventaddEventListener附加的事件显然不会出现在$('a').data('events')中。 Is there anything I can iterate in its place?有什么我可以迭代它的地方吗? I assume they're queued up somewhere, but I can't find documentation to point me in the right direction.我假设他们在某个地方排队,但我找不到文件来为我指明正确的方向。

From quirksmode :quirksmode

One problem of the current implementation of W3C's event registration model is that you can't find out if any event handlers are already registered to an element. W3C 事件注册模型当前实现的一个问题是,您无法确定是否有任何事件处理程序已注册到元素。 In the traditional model you could do:在传统模型中,您可以这样做:

 alert(element.onclick)

and you see the function that's registered to it, or undefined if nothing is registered.并且您会看到注册到它的函数,如果没有注册则未定义。 Only in its very recent DOM Level 3 Events W3C adds an eventListenerList to store a list of event handlers that are currently registered on an element.仅在其最近的DOM Level 3 事件中,W3C 添加了一个 eventListenerList 来存储当前在元素上注册的事件处理程序的列表。 This functionality is not yet supported by any browser, it's too new.任何浏览器尚不支持此功能,它太新了。 However, the problem has been addressed.但是,问题已经解决。

If you can get code installed at the beginning of the page, you can record all subsequent listeners in your own data structure with this kind of hook: Why does Google +1 record my mouse movements?如果你可以在页面的开头安装代码,你可以用这种钩子将所有后续的监听器记录在你自己的数据结构中: 为什么 Google +1 会记录我的鼠标移动? . .

I know of no way to access the existing listeners.我知道没有办法访问现有的听众。

// Introduced in DOM Level 2:
interface EventTarget {
  void               addEventListener(in DOMString type, 
                                      in EventListener listener, 
                                      in boolean useCapture);
  void               removeEventListener(in DOMString type, 
                                         in EventListener listener, 
                                         in boolean useCapture);
  boolean            dispatchEvent(in Event evt)
                                        raises(EventException);
  // Introduced in DOM Level 3:
  readonly attribute EventListenerList  eventListeners;
};

So el.eventListeners contains all event listeners attached to el using el.addEventListener .因此el.eventListeners包含使用el.addEventListener附加到el的所有事件侦听器。

You would need to use a shim to deal with browser support.您需要使用 shim 来处理浏览器支持。 Currently Chrome 12 and Firefox 5 do not support this目前 Chrome 12 和 Firefox 5 不支持这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM