DOM HTMLCollection
HTMLCollectionオブジェクト
HTMLCollectionオブジェクトは、配列のようなHTML要素のリストです。
getElementsByTagName()のようなメソッドは、HTMLCollectionを返します。
プロパティとメソッド
HTMLCollectionオブジェクトでは、次のプロパティとメソッドを使用できます。
Property / Method | Description |
---|---|
item() | Returns the element at the specified index in an HTMLCollection |
length | Returns the number of elements in an HTMLCollection |
namedItem() | Returns the element with the specified ID, or name, in an HTMLCollection |
例
例
HTMLCollectionを取得します。
var x = document.getElementsByTagName("P");
// Returns a
collection of all the P elements in the document
例
ドキュメント内の<p>要素の数を書き込みます。
var x = document.getElementsByTagName("P");
document.write(x.length);
例
HTMLCollection内のすべての要素をループします。
x = document.getElementsByTagName("*");
l = x.length;
for (i = 0; i <
l; i++) {
document.write(x[i].tagName + "<br>");
}