gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 15:23:22 25 4
gpt4 key购买 nike

我一直想知道为什么不同浏览器的 JavaScript 中事件的 target/sourceElement 的属性名称存在差异。

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

最佳答案

众所周知,Internet Explorer 是一种叛逆的浏览器,尤其是在早期版本中,它遵循自己的 Web API 标准。

尽管因此受到严厉批评,we should be also thankful with it ,因为它的反叛帮助引入了一些后来成为标准的功能,例如 innerHTML 和 AJAX 请求。

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

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 在较新的版本中开始更彻底地遵循标准,例如,attachEventcompletely deprecated in IE11 .

同样的事情也会发生在其他一些东西上,因此如果你想支持旧版本的 IE,你必须使用某些功能的回退。

至于为什么他们不在 Internet Explorer 中添加别名?嗯,他们最终有时会引入标准 API,并在一段时间内保留自己的标准,我相信这就是这种情况事件模型;例如,IE9 似乎支持 addEventListenerattachEvent

至于为什么他们制定自己的标准,我认为 the top answer in here是一个很好的解释。

关于Javascript - 事件属性的浏览器命名约定让我感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30295125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com