javascript Firefox 中的 window.event 替代方案
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22813153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
window.event alternative in Firefox
提问by Smax Smaxovi?
I see that window.event
or event
does not work in Firefox, so I need alternative for this. I don't want to set ANY HTML attributes, just Javascript.
I'm within this function and I want to get mouse coordinates from here:
我看到了,window.event
或者event
在 Firefox 中不起作用,所以我需要替代方案。我不想设置任何 HTML 属性,只是 Javascript。我在这个函数中,我想从这里获取鼠标坐标:
document.onmouseover = function(){
var mouseX = event.clientX;
var mouseY = event.clientY;
}
Obviously this won't work in firefox, so I want to know how to do it.
显然这在 Firefox 中不起作用,所以我想知道怎么做。
回答by cookie monster
This is the typical approach that you'll find in examples everywhere.
这是您可以在任何地方的示例中找到的典型方法。
document.onmouseover = function(event) {
event = event || window.event;
var mouseX = event.clientX;
var mouseY = event.clientY;
}
The W3C standard way of retrieving the event
object is via the first function parameter. Older IE didn't support that approach, so event
will be undefined
. The ||
operator lets us fetch the window.event
object in that case.
检索event
对象的 W3C 标准方法是通过第一个函数参数。较早的IE不支持这种做法,所以event
会undefined
。在这种情况下,||
运算符让我们获取window.event
对象。
回答by Samuli Hakoniemi
window.event
(which you're referring to) is a global object that isn't available in all browsers.
window.event
(您所指的)是一个并非在所有浏览器中都可用的全局对象。
document.addEventListener("mouseover", function(evt){
var mouseX = evt.clientX;
var mouseY = evt.clientY;
}, false);