意外访问“事件”变量跨浏览器? [英] Unexpected access to "event" variable across browsers?

查看:116
本文介绍了意外访问“事件”变量跨浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我开始说我明白以下代码有一个主要问题。具体来说, event 参数不会传递到函数中。我不明白是为什么在下面的代码Chrome,Opera,Safari,Firefox和IE所有处理事件变量不同。

Let me begin by saying I understand that the below code has a major issue. Specifically, the event parameter is not passed into the function. What I don't understand is why in the below code Chrome, Opera, Safari, Firefox, and IE all treat the event variable differently.

$('#eventBtn').on('click', function() {
    console.log(event);
    event.preventDefault();
});

在Chrome,Opera和Safari中,上述代码适用。 IE在第二行失败,Firefox立即失败。为了测试的目的,我创建了一个略带点缀的 jsFiddle 。上述 console.log(event)在各种浏览器中的输出:

In Chrome, Opera, and Safari the above code works. IE fails at the second line and Firefox fails immediately. For testing purposes I have created a slightly more embellished jsFiddle. The output of the above console.log(event) in the various browsers:


Chrome版本26.0.1410.64 m

MouseEvent {dataTransfer:null,toElement:button#superBtn,fromElement:null,
y:20,x:33 ...}

Chrome Version 26.0.1410.64 m
MouseEvent {dataTransfer: null, toElement: button#superBtn, fromElement: null, y: 20, x: 33…}

Opera版本12.15

MouseEvent

Opera Version 12.15
MouseEvent

Safari版本6.0.2(8536.26.17)

MouseEvent

Safari Version 6.0.2 (8536.26.17)
MouseEvent

IE版本10.0.9200.16540

[object MSEventObj]

IE Version 10.0.9200.16540
[object MSEventObj]

Firefox版本20.0.1

ReferenceError:未定义事件

Firefox Version 20.0.1
ReferenceError: event is not defined

的Chrome,Opera和Safari,因为它按预期工作而不是编码,在其他浏览器中创建意外的行为。虽然IE也有一个全局的事件变量,不像前面提到的浏览器,它不分配那个变量到当前正在触发的事件。 Firefox没有全局事件变量,因此在引用事件时失败。

I was bit by this "feature" of Chrome, Opera, and Safari because it worked as intended not as coded creating unexpected behavior in other browsers. While IE also has a global event variable, unlike the aforementioned browsers it does not assign that variable to the event that is currently firing. Firefox does not have a global event variable and therefore fails as soon as event is referenced.

通常我使用 e 来表示事件变量,这在预期中在所有浏览器中都会失败。为什么Chrome,Opera和Safari有全局事件变量,他们这样分配?这个行为记录在哪里?除了不使用事件为变量命名任何处理这个特性的建议吗?

Typically I use e for representing event variables which as expected fails the same way in all browsers. Why do Chrome, Opera and Safari have a global event variable which they assign this way? Is this behavior documented somewhere? Aside from don't use event for variable naming any advice for dealing with this "feature"?

推荐答案

IE总是提供一个全局事件变量,与W3C标准规定的不同。

IE has always provided a global event variable, unlike what the W3C standards dictate. The other browsers are doing the same in order to provide backward-compatibility with websites developed with old IE versions in mind.


虽然IE也有其他浏览器的功能,全局事件变量,与上述浏览器不同,它不会将该变量分配给当前正在触发的事件。

While IE also has a global event variable, unlike the aforementioned browsers it does not assign that variable to the event that is currently firing.

这篇关于意外访问“事件”变量跨浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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