Firefox中的window.event替代 [英] window.event alternative in Firefox

查看:226
本文介绍了Firefox中的window.event替代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现在Firefox中不能使用 window.event 事件,所以我需要替代它。我不想设置任何HTML属性,只是Javascript。
我在这个函数中,我想从这里获得鼠标坐标:
$ b $ $ $ $ $ $ $ $ $ document.onmouseover = function ){
var mouseX = event.clientX;
var mouseY = event.clientY;

$ / code>

显然这在firefox中不起作用,所以我想知道如何做到这一点。

解决方案

这是您可以在任何地方找到的典型方法。

  document.onmouseover = function(event){
event = event || window.event;

var mouseX = event.clientX;
var mouseY = event.clientY;

检索事件的W3C标准方法 code> object是通过第一个函数参数。老的IE不支持这种方法,所以 event 将会是 undefined 。在这种情况下, || 操作符让我们可以获取 window.event 对象。


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;
}

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;
}

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆