HTMLCollection: item() メソッド

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

HTMLCollectionitem() メソッドは、この集合の中で指定された位置にあるノードを返します。

メモ: HTMLCollection の中身はライブであるため、その背景にある DOM が変化すると、子の集合内にある個々のノードの位置は変化する可能性があり、インデックス値が指定されたノードを差し続けなくなる可能性があります。

構文

js
item(index)

引数

index

返す Node の位置。要素は HTMLCollection 内で、文書内のソースに現れる順序と同じ順序で現れます。

返値

指定したインデックスにある Node、または index が 0 より小さいか length プロパティ以上の場合は null です。

使用上の注意

item() メソッドは HTMLCollection から番号付けされた要素を返します。JavaScript で HTMLCollection を配列として扱い、配列記法を使用してインデックスを作成する方が簡単です。下記のを参照してください。

js
const images = document.images; // これは HTMLCollection
const img0 = images.item(0); // このように item() メソッドを使うことができる
const img1 = images[1]; // ただし、この記述方がより易しく一般的

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
item

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報