为什么window.addEventListener存在?
DOM4 说 EventTarget
有一个 addEventListener
方法和 Node
继承自 事件目标
。
现在 Document
继承自 Node< /code> 所以
document.addEventListener
的工作是有道理的。
然而Window
不是Node
。为什么 window 方法允许附加事件监听器?
当然 Window
没有提及 EventTarget
或 .addEventListener
旁白: 我为什么要关心?在编写 DOM-shim 时,我尝试模拟 window.addEventListener
得到崩溃是因为 IE8 中不存在 window.fireEvent
。我很高兴不必支持 window.addEventListener
进一步说明: 如果 window.addEventListener
是非标准的,我们是否应该尝试获取摆脱它并用 document.addEventListener
替换它?我们应该向浏览器供应商提交错误吗?
DOM4 says EventTarget
has a addEventListener
method and Node
inherits from EventTarget
.
Now Document
inherits from Node
so it makes sense that document.addEventListener
works.
However Window
is not a Node
. Why does the window method allow you to attach event listeners?
Of course Window
says nothing about EventTarget
or .addEventListener
Aside: Why do I care? Whilst writing the DOM-shim my attempts to emulate window.addEventListener
get crushed because window.fireEvent
doesn't exist in IE8. I'd be nice to not have to support window.addEventListener
Further Aside: If window.addEventListener
is non-standard should we try to get rid of it and replace it with document.addEventListener
? Should we file bugs with browser vendors?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 HTML5,Window 不是 Node,而是 EventTarget。请参阅 http://dev.w3.org/html5/spec/ browsers.html#the-window-object
According to HTML5, Window is not a Node, but it is an EventTarget. See http://dev.w3.org/html5/spec/browsers.html#the-window-object