문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
|
wiki:html:html_note:html_dom_getelementsbyclassname_method [2021/04/07 12:04] emblim98 |
wiki:html:html_note:html_dom_getelementsbyclassname_method [2023/01/13 18:44] (현재) |
||
|---|---|---|---|
| 줄 11: | 줄 11: | ||
| * [[https:// | * [[https:// | ||
| \\ | \\ | ||
| + | 지정 클래스 이름을 가진 모든 요소들을 가져옵니다.\\ | ||
| + | ====예제==== | ||
| + | <code javascript> | ||
| + | < | ||
| + | <html lang=" | ||
| + | < | ||
| + | <div class=" | ||
| + | <div class=" | ||
| + | < | ||
| + | <button onclick=" | ||
| + | < | ||
| + | < | ||
| + | function myFunction() { | ||
| + | var x = document.getElementsByClassName(" | ||
| + | x[0].innerHTML = "Hello World!"; | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | =====Definition and Usage===== | ||
| + | %%getElementsByClassName()%% 메서드는 지정된 클래스 이름을 가진 문서의 모든 요소 컬렉션을 %%HTMLCollection%% 오브젝트로 반환합니다.\\ | ||
| + | \\ | ||
| + | %%HTMLCollection%% 오브젝트는 __노드 컬렉션__< | ||
| + | 노드는 인덱스 번호로 액세스할 수 있습니다. 인덱스는 0에서 시작합니다.\\ | ||
| + | \\ | ||
| + | **Tip:**\\ | ||
| + | %%HTMLCollection%% 개체의 길이 속성을 사용하여 지정된 클래스 이름을 가진 요소의 수를 확인한 다음\\ | ||
| + | 모든 요소를 반복하고 원하는 정보를 추출할 수 있습니다.\\ | ||
| + | |||
| + | =====Syntax===== | ||
| + | <code javascript> | ||
| + | document.getElementsByClassName(classname) | ||
| + | </ | ||
| + | |||
| + | =====Parameter Values===== | ||
| + | | Parameter | ||
| + | ^ classname | ||
| + | ^ ::: | ||
| + | |||
| + | =====Technical Details===== | ||
| + | 반환 값:\\ | ||
| + | 반환하는 값은 %%HTMLCollection%% 오브젝트이며, | ||
| + | 반환된 컬렉션의 요소들은 소스 코드에서 보여지는 대로 정렬됩니다.\\ | ||
| + | |||
| + | ====예제==== | ||
| + | " | ||
| + | <code javascript> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | div { | ||
| + | border: 1px solid #000; | ||
| + | margin: 5px; | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | < | ||
| + | <div class=" | ||
| + | <p>P element in first div with class=" | ||
| + | </ | ||
| + | <div class=" | ||
| + | <p>P element in first div wikth class=" | ||
| + | </ | ||
| + | <div class=" | ||
| + | <p>P element in second div with class=" | ||
| + | </ | ||
| + | < | ||
| + | <button onclick=" | ||
| + | < | ||
| + | < | ||
| + | function myFunction() { | ||
| + | var x = document.getElementsByClassName(" | ||
| + | x[0].style.backgroundColor = " | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ====예제==== | ||
| + | %%HTMLCollection%% 오브젝트의 length 속성을 사용하여, | ||
| + | <code javascript> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <div class=" | ||
| + | A div element with class=" | ||
| + | </ | ||
| + | <div class=" | ||
| + | Another div element with class=" | ||
| + | </ | ||
| + | <p class=" | ||
| + | < | ||
| + | <button onclick=" | ||
| + | < | ||
| + | <p id=" | ||
| + | < | ||
| + | function myFunction() { | ||
| + | var x = document.getElementsByClassName(" | ||
| + | document.getElementById(" | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ====예제==== | ||
| + | %%class=" | ||
| + | <code javascript> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | .example { | ||
| + | border: 1px solid #000; | ||
| + | margin: 5px; | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | < | ||
| + | <div class=" | ||
| + | A div with class=" | ||
| + | </ | ||
| + | <div class=" | ||
| + | Another div with class=" | ||
| + | </ | ||
| + | <p class=" | ||
| + | < | ||
| + | < | ||
| + | <button class=" | ||
| + | < | ||
| + | < | ||
| + | function myFunction() { | ||
| + | var x = document.getElementsByClassName(" | ||
| + | var i; | ||
| + | for (i = 0; i < x.length; i++) { | ||
| + | x[i].style.backgroundColor = " | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| \\ | \\ | ||
| \\ | \\ | ||
| {{tag> | {{tag> | ||