프로그래밍/Javascript

getElementsByClassName() / getElementsByTagName()

matte 2020. 12. 25. 13:43

getElementsByClassName()

The Element method getElementsByClassName() returns a live HTMLCollection which contains every descendant element which has the specified class name or names

 

Parameters

A Domstring containing one or more class names to match on, separated by whitespace

 

Return value

An HTMLCollection providing a live-updating list of every element which is a member of every class in names.

var elements = document.getElementsByClassName('test');
var elements = element.getElementsByClassName('test red');
var elements = document.getElementById('main').getElementsByClassName('test');

 

getElementsByClassName()

The Element.getElementsByTagName() method returns a live HTMLCollection of elements with the given tag name. All descendants of the specified element are searched, but not the element itself. 

 

The HTMLCollection interface represents a generic collection (array-like object) of elements.

내부 요소를 가져올 때, 요소가 1개 이든, 여러 개이든 상관없이, 반복문으로 처리해줘야 함 for 문이든, each 든, while 이든 ... HTMLCollection 에는 item 이라는 메소드도 있음

var matches = element.getElementsByClassName('colorbox');

for( var i=0; i<matches.length ; i++ ) {
	matches[i].어쩌구저쩌구조금더확인해보기;
}

 

developer.mozilla.org/en-US/docs/Web/API/Element/getElementsByClassName

 

Element.getElementsByClassName() - Web APIs | MDN

The Element method getElementsByClassName() returns a live HTMLCollection which contains every descendant element which has the specified class name or names. The method getElementsByClassName() on the Document interface works essentially the same way, exc

developer.mozilla.org