NodeList.item
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
概述
根据给定的索引,返回一个 NodeList对象中包含的 Node 对象。
语法
nodeItem = nodeList.item(index)
nodeList是一个NodeList对象。通常是由某个 DOM 属性或方法返回的,比如childNodes.index是给定的索引。从 0 开始。nodeItem是根据索引index由item方法从nodeList中获取到的节点。
JavaScript 有更简单的语法来从一个 NodeList 对象中获取指定索引的节点:
nodeItem = nodeList[index]
例子
var tables = document.getElementsByTagName("table");
var firstTable = tables.item(1); // 或者简写为 tables[1],返回一个文档中的第二个 table 元素。
备注
如果索引越界,该方法不会抛出异常,只会返回 null。
item() 不是 DOM 元素或者 DOM 节点的方法,而是 NodeList 对象的方法。