2016-08-25 40 views

回答

2

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的命名属性来访问。