此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

HTMLCollection.item

基线 广泛可用

自 2015年7月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

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

参数

index

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

返回值

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

Description

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

Example

js
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