→、
Document 内の指定クラス名をすべて持つすべての要素のコレクションを HTMLCollection オブジェクトで返します。
任意の要素に対して Element.getElementsByClassName() を呼び出すこともできます。その場合は、指定されたルート要素下の指定されたクラス名を持つ要素だけを返します。
| Document | Document。 |
|---|---|
| names | 文字列。一致させるクラス名を表します。複数のクラス名はホワイト スペースで区切ります。 |
見つかった要素の生きた HTMLCollection です。
'test' クラスを持つすべての要素を取得します。
document.getElementsByClassName('test')
'red' および 'test' クラスを両方持つすべての要素を取得します。
document.getElementsByClassName('red test')
¶他例省く
HTML:
<SPAN class=Test>SPAN要素</SPAN><BR> <SPAN class="Test Test2">SPAN要素2</SPAN>
JavaScript:
D=document.getElementsByClassName("Test") //=[object]:[object HTMLCollection]
//0=[object]:[object HTMLSpanElement]
//1=[object]:[object HTMLSpanElement]
//length=[number]:2
//item=[function]:function item() { [native code] }
//namedItem=[function]:function namedItem() { [native code] }
D=document.getElementsByClassName("Test2") //=[object]:[object HTMLCollection]
//0=[object]:[object HTMLSpanElement]
//length=[number]:1
//item=[function]:function item() { [native code] }
//namedItem=[function]:function namedItem() { [native code] }
D=document.getElementsByClassName("T") //=[object]:[object HTMLCollection]
//length=[number]:0
//item=[function]:function item() { [native code] }
//namedItem=[function]:function namedItem() { [native code] }