简体   繁体   English

Javascript-事件属性的浏览器命名约定使我感到困惑

[英]Javascript - Browser naming conventions for event attributes confusing me

I've been wondering why there is a difference in the attribute name for an event's target/sourceElement in JavaScript for different browsers. 我一直想知道为什么不同浏览器的JavaScript中事件的target / sourceElement的属性名称有所不同。

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

Internet Explorer is known to be a rebellious browser, especially in early versions, going by its own standards with the web API. Internet Explorer众所周知是一种反叛的浏览器,尤其是在早期版本中,它以其自己的标准与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. 尽管它为此受到了严厉批评, 但我们也应该对此表示感谢 ,因为它的反叛帮助引入了一些后来成为标准的功能,例如innerHTML和AJAX请求。

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. 在这种特定情况下,约定的差异会出现,因为较旧版本的IE使用的事件模型与其他浏览器使用的标准模型不同。 With it, comes a series of differently named objects. 随之而来的是一系列不同名称的对象。 From MDN : MDN

On IE6-8, the event model is different. 在IE6-8上,事件模型有所不同。 Event listeners are attached with the non-standard element.attachEvent() method. 事件侦听器附带有非标准的element.attachEvent()方法。 In this model, the event object has an srcElement property, instead of the target property, and it has the same semantics as event.target. 在此模型中,事件对象具有srcElement属性而不是target属性,并且其语义与event.target相同。

Internet Explorer started following standards more thoroughly in more recent versions, for instance, attachEvent is completely deprecated in IE11 . Internet Explorer在较新的版本中开始更全面地遵循标准,例如, IE11中完全弃用了 attachEvent

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. 同样的事情也发生在其他一些东西上,因此,如果要支持IE的较早版本,为什么必须使用具有某些功能的后备。

As to why they don't add aliases in Internet Explorer? 至于为什么他们不在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; 好吧,他们最终有时会引入标准API并保留自己的标准一段时间,我相信事件模型就是这种情况。 It seems, for instance, that IE9 supports both addEventListener and attachEvent . 例如,似乎IE9同时支持addEventListenerattachEvent

As to why they make their own standards, I think the top answer in here is a good explanation. 至于为什么他们要制定自己的标准,我认为这里的最佳答案是一个很好的解释。

Every time you load up a web page, the browser is turning the code it gets into a usable format which your computer understands. 每次加载网页时,浏览器都会将代码转换为计算机可以理解的可用格式。 However the different browsers do it in slightly different ways, so sometimes exceptions need to be made for certain browsers. 但是,不同的浏览器的执行方式略有不同,因此有时需要为某些浏览器设置例外。

The browser is a translator, sometimes the translation can be different depending on who does the translation. 浏览器是翻译器,有时翻译可能会有所不同,具体取决于谁进行翻译。

In this specific case I would guess Internet explorer understand srcElement, while all the others understand target. 在这种特定情况下,我猜想Internet Explorer可以理解srcElement,而其他所有对象都可以理解目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM