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① |
瀏覽器相容性
載入中…