属性 "getter object (DOMString name);" 在文档 IDL 中

property "getter object (DOMString name);" in the Document IDL

HTML 5.1 Specification 在文档 IDL 中定义奇怪的 属性。

getter object (DOMString name);

这不是打字错误,我不知道如何使用它。

Document 接口的 WebIDL 定义部分指定它有一个 named property getter. It only has meaning in combination with the section of the spec of the HTML spec that defines the supported property names for the Document interface.

它们共同指定了一些作为 Document.

的命名属性公开的内容

考虑以下文档:

<!doctype html>
<form name=foo></form>
<form name=bar></form>
<iframe name=bar></iframe>
<p id=baz>

如果您调用 document.foo,您将返回一个元素,即 form name=foo 元素。

如果您调用 document.bar,您将得到一个包含 form name=bar 元素和 iframe name=bar 元素的集合。

如果你打电话给 document.baz 你会回来 undefined

所有这些行为的原因是,section of the HTML spec defining the supported property names for the Document interface 指定 form[name] 值和 iframe[name] 值可作为 Document[=44= 的命名属性访问]

that spec section也表示,如果一个名为属性的Document只匹配一个元素,则返回该元素,但如果匹配多个元素,则返回一个集合.

document.baz returns undefined 的原因是因为 that spec section 没有指定 p[id] 值作为 [=11 的命名属性可访问=].

但是,如果您改为执行 window.baz,您 取回 p id=baz 元素。

造成这种差异的原因是:虽然 WebIDL definition for Window specifies it is having a named property getter (just as the Document WebIDL does), the section defining the supported property names for Window——与 Document 的类似部分不同—— 指定了 p[id] 值(实际上 任何 元素的 id 值)作为 Window.

的命名属性可访问