From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/htmlcollection/index.html | 66 ++++++++++++++++++++++ files/zh-cn/web/api/htmlcollection/item/index.html | 36 ++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 files/zh-cn/web/api/htmlcollection/index.html create mode 100644 files/zh-cn/web/api/htmlcollection/item/index.html (limited to 'files/zh-cn/web/api/htmlcollection') diff --git a/files/zh-cn/web/api/htmlcollection/index.html b/files/zh-cn/web/api/htmlcollection/index.html new file mode 100644 index 0000000000..07358016cb --- /dev/null +++ b/files/zh-cn/web/api/htmlcollection/index.html @@ -0,0 +1,66 @@ +--- +title: HTMLCollection +slug: Web/API/HTMLCollection +translation_of: Web/API/HTMLCollection +--- +

{{ APIRef("HTML DOM") }}

+ +

HTMLCollection 接口表示一个包含了元素(元素顺序为文档流中的顺序)的通用集合(generic collection),还提供了用来从该集合中选择元素的方法和属性。

+ +
注意:由于历史原因(DOM4之前,实现该接口的集合只能包含 HTML 元素),该接口被称为 HTMLCollection
+ +

HTML DOM 中的 HTMLCollection 是即时更新的(live);当其所包含的文档结构发生改变时,它会自动更新。

+ +

属性

+ +
+
{{domxref("HTMLCollection.length")}} {{readonlyInline}}
+
返回集合当中子元素的数目。
+
+ +

方法

+ +
+
{{domxref("HTMLCollection.item()")}}
+
根据给定的索引(从0开始),返回具体的节点。如果索引超出了范围,则返回 null
+
{{domxref("HTMLCollection.namedItem()")}}
+
根据 Id 返回指定节点,或者作为备用,根据字符串所表示的 name 属性来匹配。根据 name 匹配只能作为最后的依赖,并且只有当被引用的元素支持 name 属性时才能被匹配。如果不存在符合给定 name 的节点,则返回 null
+
+ +

在 JavaScript 中使用

+ +

在 JavaScript 中,为了获取给定的 HTMLCollection 的元素,可以使用方括号语法来代替直接调用 item()namedItem() 方法。在方括号中,数值如同 item(),字符串值如同 namedItem()。

+ +

例如,假定在文档中有一个 <form> 元素,且它的 id 是 "myForm"

+ +
var elem1, elem2;
+
+// document.forms 是一个 HTMLCollection
+
+elem1 = document.forms[0];
+elem2 = document.forms.item(0);
+
+alert(elem1 === elem2); // 显示 "true"
+
+elem1 = document.forms["myForm"];
+elem2 = document.forms.namedItem("myForm");
+
+alert(elem1 === elem2); // 显示 "true"
+ +

浏览器兼容性

+ +

当使用字符串作为 namedItem 的参数,且匹配的元素多于一个时,不同的浏览器表现不同。Firefox 8 表现如同 DOM 2 和 DOM 4 说明的,返回第一个匹配的元素。而 Webkit 浏览器和 IE 返回另外一个 HTMLCollection,Opera 返回一个包含所有元素的 {{domxref("NodeList")}}。

+ +

规范

+ + + +

相关链接

+ + diff --git a/files/zh-cn/web/api/htmlcollection/item/index.html b/files/zh-cn/web/api/htmlcollection/item/index.html new file mode 100644 index 0000000000..4d1ad8a5c8 --- /dev/null +++ b/files/zh-cn/web/api/htmlcollection/item/index.html @@ -0,0 +1,36 @@ +--- +title: HTMLCollection.item +slug: Web/API/HTMLCollection/item +translation_of: Web/API/HTMLCollection/item +--- +

{{APIRef("HTML DOM")}}

+ +

HTMLCollection.item() 由位置获取元素.

+ +

参数

+ +
+
index
+
想要被返回的Node的位置. 元素在HTML Collection中的顺序和他们在源文档的顺序保持一致。
+
+ +

返回值

+ +

指定的index的{{domxref("Node")}} , 如果index小于0或者不小于它的长度属性则返回null。

+ +

Description

+ +

HTMLCollection中item( )方法返回一个编号的元素 ,在JavaScript中把HTMLCollection当成是一个是数组并用数组符号去索引十分简单。

+ +

Example

+ +
var c = document.images;  // This is an HTMLCollection
+var img0 = c.item(0);     // You can use the item( ) method this way
+var img1 = c[1];          // But this notation is easier and more common
+
+ +

See also

+ + -- cgit v1.2.3-54-g00ecf