Firefox 中的 window.event 替代方案 [英] window.event alternative in Firefox
问题描述
我发现 window.event
或 event
在 Firefox 中不起作用,所以我需要替代方案.我不想设置任何 HTML 属性,只是 Javascript.我在这个函数中,我想从这里获取鼠标坐标:
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:
document.onmouseover = function(){
var mouseX = event.clientX;
var mouseY = event.clientY;
}
显然这在 Firefox 中不起作用,所以我想知道如何去做.
Obviously this won't work in firefox, so I want to know how to do it.
推荐答案
这是典型的方法,您可以在随处可见的示例中找到.
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;
}
检索 event
对象的 W3C 标准方法是通过第一个函数参数.较旧的 IE 不支持这种方法,因此 event
将是 undefined
.在这种情况下,||
运算符让我们可以获取 window.event
对象.
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.
这篇关于Firefox 中的 window.event 替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!