HTML 5.1 Specification中定义Document IDL中的奇怪属性。属性“getter对象(DOMString名称);”在Document IDL
getter object (DOMString name);
这不是一个错字,我不明白如何使用它。
HTML 5.1 Specification中定义Document IDL中的奇怪属性。属性“getter对象(DOMString名称);”在Document IDL
getter object (DOMString name);
这不是一个错字,我不明白如何使用它。
Document
接口的WebIDL定义的那部分指定它具有named property getter。它只与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
而且that spec section命名属性访问也说,如果一个Document
命名的属性只有一个元素相匹配,那么元素被返回,但是如果它匹配多个元素,则返回一个集合。
其原因document.baz
回报undefined
是因为that spec section未指定p[id]
值是为Document
的命名属性访问。
但是,如果您改为window.baz
您将取回p id=baz
元素。
其原因差别是:当指定WebIDL definition for Window
它具有named property getter(正如Document
WebIDL一样),则section defining the supported property names for Window
-unlike类似部分,用于Document
- 并指定p[id]
值(实际上的id
值任何元素)可作为Window
的命名属性来访问。