var elem=document.getElementById('mydiv');
elem.addEventListener('click',function(){...});
执行上述代码后,elem
是HTMLDivElement 接口(interface)的一个实例。我的大问题是 addEventListener() 方法到底做了什么。它在哪些 DOM 对象中注册监听器以及它是如何注册的(它更改了这些 DOM 对象的哪些属性)。换句话说,我想知道 elem
是如何被告知添加了一个监听器的,它的哪些属性(所有属性都到它的原型(prototype)链)受到了影响。例如,我知道 Event.prototype 具有重要的属性,如 type
、target
;但是我无法将它们与 elem
...
我不想查找哪些事件监听器附加到上述 DOM 节点。我想知道内部程序。
谢谢
最佳答案
After execution of the above code elem is an instance of
HTMLDivElement
interface. My big question is what exactlyaddEventListener()
method does.In which DOM objects does it register the listener
在调用 addEventListener
的 DOM 元素上。 (当然,子元素上的事件可能会冒泡)。
and how it does that (which properties of these DOM objects it changes)
它是如何做到的是一个内部实现细节。它不会更改 DOM 对象的用户可见属性。
In other words, I'd like to know how elem is informed about the addition of a listener
不是。
which of its properties (all of them down to its prototype chain) are affected
都没有。
For example I know that Event.prototype has crucial properties like type, target; however I cannot "connect" them with elem...
这些是Event
的属性,与elem
无关。
关于javascript - addEventListener 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33914044/