我何时应该在窗口与文档与document.body上观察javascript事件? [英] When should I observe javascript events on window vs. document vs. document.body?
问题描述
我正在使用prototype.js作为我的网络应用程序,并且我已经在chrome,safari和firefox上运行了所有内容。我现在正致力于IE8兼容性。
I'm using prototype.js for my web app, and I have everything running on chrome, safari, and firefox. I am now working on IE8 compatibility.
因为我在IE中进行了调试,所以我注意到有一些javascript事件,我之前已经设置了一个观察者窗口,例如
As I've been debugging in IE, I've noticed that there are javascript events for which I have previously set an observer on the window, e.g.
Event.observe(window, eventType, function () {...});
(其中 eventType
可能dom:loaded
,keypress
等等。它在Chrome / Safari / Firefox中运行良好。然而,在IE中,观察者永远不会发射。
(where eventType
might be "dom:loaded"
, "keypress"
, etc.) and it works just fine in Chrome/Safari/Firefox. However, in IE the observer never fires.
至少在某些情况下,我可以通过将观察者放在除窗口
,例如 document
(在dom:loaded
的情况下)或 document.body
(在keypress
的情况下)。但是,这都是反复试验。
In at least some cases I could get this to work on IE by instead placing the observer on something other than window
, e.g. document
(in the case of "dom:loaded"
) or document.body
(in the case of "keypress"
). However, this is all trial-and-error.
是否有更系统的方法来确定这些观察者的位置,以便结果与浏览器兼容?
Is there some more systematic way to determine where to place these observers such that the results will be cross-browser compatible?
谢谢!
推荐答案
各种浏览器的对象文档(例如 window
在MSDN上, document
on MDC)定义对象支持哪些事件。你可以从那里开始。
The various browsers' object documentation (e.g. window
on MSDN, document
on MDC) define which events are supported on the object. You could start there.
这篇关于我何时应该在窗口与文档与document.body上观察javascript事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!