NodeList: item() 方法
通过索引从 NodeList 返回一个节点。只要提供了参数,此方法就不会抛出异常。如果索引超出范围,则返回 null;如果未提供参数,则抛出 TypeError。
在 JavaScript 中,除了调用 nodeList.item(index) 之外,还可以直接访问 index,例如 nodeList[index]。
语法
js
item(index)
参数
index-
要获取的节点的索引。索引是基于零的。
返回值
由 item 方法返回的 nodeList 中的第 index 个节点。
异常
TypeError-
如果未提供参数,则会抛出此异常。
示例
js
const tables = document.getElementsByTagName("table");
const firstTable = tables.item(1); // or tables[1] - returns the second table in the DOM
规范
| 规范 |
|---|
| DOM # ref-for-dom-nodelist-item① |
浏览器兼容性
加载中…