JavaScript

[DOM_HTMLCollection] JS - length 속성 ★ - 요소개수 반환 (※ for반복문에 활용)

length 예제

 

<h1>홈짱닷컴 Homzzang.com</h1>

<p>홈페이지 제작관리 + 서버관리</p>

<p>HTML CSS JS JQ PHP SQL BS</p>


<button onclick="homzzang()">클릭</button>


<script>

function homzzang() {

  var n = document.getElementsByTagName("P").length;

  alert(n);

}

</script>

 

결과보기

 

 

length 정의

 

HTMLCollection 요소 개수

 


 

1.

읽기 전용.

 

2.

반복문 작성할 때 유용.

 

3.

모든 브라우저 지원.

 

 

 

length 구문

 

HTMLCollection.length

 


 

[반환값]

HTMLCollection 요소 개수

 

 

 

length 예제 - 반복문

 

<h1>홈짱닷컴 Homzzang.com</h1>

<p class='hz'>홈페이지 제작관리 + 서버관리</p>

<p class='hz'>HTML CSS JS JQ PHP SQL BS</p>

<p>그누보드 제작관리 강의</p>


<button onclick="homzzang()">클릭</button>


<script>

function homzzang() {

  var x, i;

  x = document.getElementsByClassName("hz");

  for (i = 0; i < x.length; i++) {

    x[i].style.backgroundColor = "yellow";

  }

}

</script>

 

결과보기

 

 


분류 제목
DOM_Event_Object JS - StorageEvent 객체 - 윈도우창 저장영역 변경 (= 스토리지이벤트객체= 스토리지이벤트종류)
DOM_Event_Object JS - TouchEvent 객체 - 터치이벤트객체 (= 터치이벤트종류)
DOM_Event_Object JS - TransitionEvent 객체 - CSS 전환 실행 (= 트랜지션이벤트객체 = 트랜지션이벤트종류…
DOM_Event_Object JS - UiEvent 객체 - 유저인터페이스변환 (= 유아이이벤트객체 = 유저인터페이스이벤트종류 = 사용자…
DOM_Event_Object JS - WheelEvent 객체 - 마우스휠움직임 (= 휠이벤트객체)
API_Geolocation JS - coordinates -
API_Geolocation JS - position -
API_Geolocation JS - positionError -
API_Geolocation JS - positionOptions -
API_Geolocation JS - clearWatch() 메서드 -
API_Geolocation JS - getCurrentPosition() 메서드 -
API_Geolocation JS - watchPosition() 메서드 -
Window_History JS - history.length 속성 - history개수 (= 방문이력개수 = 방문주소개수 = 히스토리…
Window_History JS - history.back() 메서드 ★ - 이전페이지 가기
Window_History JS - history.forward() 메서드 - 다음페이지 가기
53/89
목록
 홈  PC버전 로그인 일본어
그누앞단언어
그누뒷단언어
그외코딩언어
그누보드
제작의뢰
Q&A
커뮤니티
웹유틸
회원센터
홈짱닷컴 PC버전 로그인