NodeList: item() 方法

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

透過索引從 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①

瀏覽器相容性