添加 name 属性会导致 ID 值成为 IE 和 Opera 中文档对象的属性吗?
考虑这个 HTML 源代码:
<form id="foo1" name="x"> Form 1 </form>
<form id="foo2"> Form 2 </form>
如您所见,我们定义了两个 FORM 元素。
在 Chrome、Safari 和 Firefox 中,document.foo1
和 document.foo2
均返回 undefined
。
但是,在 IE 和 Opera 中,document.foo1
返回对相应 FORM 元素的引用,而 document.foo2
返回 undefined
。
现场演示: http://jsfiddle.net/zrmEm/2/
因此,第一个表单在文档对象中确实有其 ID 命名的属性,而第二个表单则没有。这种差异是在第一个表单中添加 name
属性的结果。
现在,其中的逻辑在哪里?这是已知行为吗?
Consider this HTML source code:
<form id="foo1" name="x"> Form 1 </form>
<form id="foo2"> Form 2 </form>
As you can see, we define two FORM elements.
In Chrome, Safari and Firefox, both document.foo1
and document.foo2
return undefined
.
However, in IE and Opera, document.foo1
returns a reference to the corresponding FORM element, whereas document.foo2
returns undefined
.
Live demo: http://jsfiddle.net/zrmEm/2/
So, the first form does have its ID-named property in the document object, and the second form doesn't. And this difference is the result of adding the name
attribute to the first form.
Now, where's the logic in that? Is this a known behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在所有浏览器中,表单的 name 属性的值会作为文档对象的属性添加,并且还会作为 document.forms 的属性添加 集合。
如果您使用 ids,则这些值仅作为 forms 集合的属性添加。
IE 总是对 name 和 id 属性感到困惑。在 IE(已测试版本 8)中,仅如果该表单还有一个名称(可能与 id 相同或不同),则将 id 添加为文档的属性。
因此,如果您始终使用 document.forms['name-or-id'],那么生活就会很甜蜜。只要您的表单名称不与其他具有 id 的表单相同,那么所有的赌注都会被取消。
In all browsers, the value of a form's name attribute is added as a property of the document object, and also as a property of the document.forms collection.
If you are using ids, the values are added only as properties of the forms collection.
IE was always confused about name and id attributes. In IE (version 8 tested), the id is added a property of the document only if there is also a name for that form (it might be the same or different to the id).
So if you always use document.forms['name-or-id'] then life is sweet. As long as you don't have forms with names that are the same as other forms with ids, then all bets are off.
这基本上是微软在浏览器战争的黑暗时期引入的一个怪癖。 (大约 IE4)
最终结果是,在 IE 中,表单将作为变量添加到文档范围中,以便您可以将它们引用为
document.form1
。这是非标准的,但当时这似乎并不重要(至少对浏览器供应商而言)。
当时添加到一种浏览器或另一种浏览器中的许多非标准功能最终被其他浏览器实现,成为事实上的标准,并最终成为官方标准。
然而,这个特殊功能却没有。它仍然是非标准的。
即使在较新版本的浏览器中,微软也保留了大部分旧的非标准功能,以努力保留旧代码的向后兼容性(许多企业内部网是由微软培训的“专家”编写的,并使用这些功能,因此他们需要保留它们,否则没有人会升级到 IE6 以上)。
那时,Opera 是浏览器世界中年轻的觊觎者。它经常更新,具有创新功能,并且运行速度比竞争对手快得多。他们当时为浏览器世界做的事情就像 Chrome 最近所做的那样。
然而,由于标准之战不断升级,Opera 唯一的竞争方式就是兼容,他们不遗余力地对 IE 中的所有功能(甚至在某些情况下是错误)进行逆向工程,以便为 IE 编写的网站能够也在歌剧院工作。
这显然是这些功能之一,这就是为什么 IE 和 Opera 今天都有同样的怪癖。
当今市场上的其他浏览器没有相同的历史。 Firefox 最终源自 Netscape Navigator,因此虽然它可能有自己的怪癖,但它不会分享 IE 的怪癖。而且 webkit 浏览器的起源要晚得多(它源自 KDE 项目的 KHTML 引擎),因此它从来没有尝试模仿 IE 的怪癖的历史。
This is basically a quirk that Microsoft introduced way back in the dark days of the browser wars. (circa IE4)
The end result is that in IE, forms would be added as variables to the document scope, so that you could reference them as
document.form1
.This was non-standard, but that didn't really seem to matter back then (at least not to the browser vendors).
Many of the non-standard features that were added at the time to one browser or another ended up being implemented by the others, became de-facto standards, and eventually ended up as official standards.
This particular feature, however, did not. It remains non-standard.
Microsoft have retained most of their old non-standard functionality even in newer versions of their browser, in an effort to retain backward compatibility for old code (many corporate intranets were written by Microsoft-trained 'experts', and use these features, so they need to keep them, otherwise no-one will ever upgrade past IE6).
Back in those days, Opera was the young pretender in the browser world. It was updated often, had innovative features, and ran much quicker than the competition. They were doing back then for the browser world what Chrome has been doing more recently.
However because of the escalating standards war, the only way Opera could compete was by being compatible, and they went to great lengths to reverse-engineer all the features (and even in some cases the bugs) in IE so that sites written for IE would also work in Opera.
This was clearly one of those features, and this is why IE and Opera both share the same quirk today.
The other browsers on the market today do not have the same history. Firefox is ultimately derived from Netscape Navigator, so while it may have its own quirks dating back to those days, but it won't share IE's. And the webkit browsers have a much more recent provenance (it is derived from the KDE Project's KHTML engine), so again it never had the history where it was trying to emulate IE's quirks.