DOM HTML 컬렉션
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>");
}