为什么在事件对象中存在微妙的跨浏览器差异 [英] Why the subtle cross-browser differences in Event Object

查看:122
本文介绍了为什么在事件对象中存在微妙的跨浏览器差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在窗口级别的以下声明:

The following declaration at the window level:


    var event; // for IE
    var event = "anything"; // for Chrome

将销毁此处使用的事件对象:

will destroy the event object as used here:


    <div onMouseOver = "alert(event.type);">Mouseover Div</div>

Firefox似乎没有分阶段声明。

Firefox does not seem phased by either declaration.

一个名为event的变量是错误的代码,但我很好奇这里的技术差异,例如在IE中使用var会将变量重新初始化为null,而Chrome不会使用var声明覆盖,除非显式地分配了一个值,也许FF将窗口的var声明范围外的事件对象保存在一起。

I realize that declaring a variable with the name "event" is bad code but I am curious about the technical difference here, e.g. that the use of var in IE reinitializes the variable to null, whereas Chrome will not overwrite with a var declaration unless a value is explicitly assigned, and maybe FF holds the event object outside of the window's var declaration scope altogether.

这更是一个好奇心。我遇到了一个在我的控制之外的网站上的一个错误是由这引起的,更多的我看到更多,我看到浏览器之间的微妙差异。

This is more of a curiosity. I ran into a bug in IE on a site outside of my control that was caused by this and the more I looked into the more I saw subtle differences between browsers. Just wondering if anyone had any insights here.

推荐答案

在IE中, event 窗口对象的属性,用于事件处理函数访问正在处理的事件。在其他浏览器(如Firefox)中,情况是在事件处理程序属性中,属性内部的JavaScript代码被调用,因为它形成函数的主体,已经向其传递了一个参数 event 对应于正在处理的事件。因此,在

In IE, event is a property of the window object and is used in event handlers functions to access the event being handled. In other browsers such as Firefox, the situation is that in an event handler attribute, the JavaScript code inside the attribute is called as though it forms the body of a function into which has been passed a parameter called event that corresponds to the event being handled. So in

<div onmouseover="alert(event.type);">Mouseover Div</div>

鼠标悬停代码有效

function(event) {
    alert(event.type);
}

事件参数覆盖在包含作用域中声明的任何事件,而在IE中,

and the event parameter overrides any event declared in a containing scope, whereas in IE, it's

function() {
    alert(event.type);
}

事件标识符被解析为全局对象的属性(即 window )。

and the event identifier is resolved as a property of the global object (i.e. window).

这篇关于为什么在事件对象中存在微妙的跨浏览器差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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