Javascript-事件属性的浏览器命名约定使我感到困惑 [英] Javascript - Browser naming conventions for event attributes confusing me

查看:86
本文介绍了Javascript-事件属性的浏览器命名约定使我感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想知道为什么在不同浏览器的JavaScript中,事件的target/sourceElement的属性名称有所不同.

I've been wondering why there is a difference in the attribute name for an event's target/sourceElement in JavaScript for different browsers.

event.srcElement in Internet Explorer
event.target in most other browsers.

推荐答案

众所周知,Internet Explorer是一种反叛的浏览器,尤其是在早期版本中,它以其自己的标准与Web API兼容.

Internet Explorer is known to be a rebellious browser, especially in early versions, going by its own standards with the web API.

尽管对此进行了严厉批评,但

Although it is heavily criticized for this, we should be also thankful with it, since its rebellion has helped introduce some features that went on to become a standard, such as innerHTML and AJAX requests.

在这种特殊情况下,约定的差异会出现,因为较旧版本的IE使用的事件模型与其他浏览器使用的标准模型不同.随之而来的是一系列不同名称的对象. 来自 MDN :

In this particular case, the difference in convention arises because older versions of IE use a different event model than the standard model other browsers use. With it, comes a series of differently named objects. From MDN:

在IE6-8上,事件模型不同.附加了事件侦听器 使用非标准element.attachEvent()方法.在此模型中, 事件对象具有srcElement属性,而不是目标 属性,并且具有与event.target相同的语义.

On IE6-8, the event model is different. Event listeners are attached with the non-standard element.attachEvent() method. In this model, the event object has an srcElement property, instead of the target property, and it has the same semantics as event.target.

Internet Explorer在更新的版本中开始更完全地遵循 标准,例如,attachEvent

Internet Explorer started following standards more thoroughly in more recent versions, for instance, attachEvent is completely deprecated in IE11.

同一件事也发生在其他一些东西上,因此,如果要支持IE的较早版本,为什么必须使用具有某些功能的后备.

This same thing happens with some other stuff, hence why you've got to use fallbacks with certain features, if you want to support older versions of IE.

关于为什么不在Internet Explorer中添加别名的原因?那么,他们最终有时会引入标准API并保留一段时间以保持自己的标准,我认为情况就是如此.事件模型;例如,似乎IE9支持addEventListenerattachEvent.

As to why they don't add aliases in Internet Explorer? Well, they eventually and sometimes introduce the standard API and keep their own standard for a while, which I believe is the case with the event model; It seems, for instance, that IE9 supports both addEventListener and attachEvent.

关于为什么,他们制定了自己的标准,我认为

As to why they make their own standards, I think the top answer in here is a good explanation.

这篇关于Javascript-事件属性的浏览器命名约定使我感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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