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_Element JS - querySelectorAll() 메서드 - Element객체의 쿼리선택자_전부 (= 쿼리선택자_모…
DOM_Element JS - removeAttribute() 메서드 - 요소의 지정 속성 제거 (= removeAttribute…
DOM_Element JS - removeAttributeNode() 메서드 - 속성노드제거
DOM_Element JS - removeChild() 메서드 - 자식노드제거
DOM_Element JS - removeEventListener() 메서드 ★ - 이벤트핸들러를 요소에서 제거 (IE9 이상)
DOM_Element JS - replaceChild() 메서드 - 자식노드 대체 (= replaceChild메서드 = 리플레이스…
DOM_Element JS - requestFullscreen() 메서드 - 전체화면모드로 열기 (IE11 이상. 접두어필요)
DOM_Element JS - scrollHeight 속성 - 스크롤높이 (= scrollHeight속성 = 스크롤하이트속성) (…
DOM_Element JS - scrollIntoView() 메서드 - 지정요소가 보이는 곳으로 스크롤이동 (IE8 이상)
DOM_Element JS - scrollLeft 속성 ★ - 스크롤수평위치 (= 스크롤좌우위치) ※ 새로고침 가로 스크롤바 오른…
DOM_Element JS - scrollTop 속성 ★ - 스크롤수직위치 (= 스크롤상하위치 = scrollTop속성 = 스크롤…
DOM_Element JS - scrollWidth 속성 - 스크롤수평너비 (= 스크롤수평길이 = 스크롤가로너비 = 스크롤가로길이…
DOM_Element JS - setAttribute() 메서드 - 속성설정 (※ 속성복사 가능) (= setAttribute메서…
DOM_Element JS - setAttributeNode() 메서드 ★ - 속성노드 추가설정 (예: 클래스추가) (= setA…
DOM_Element JS - style 속성 ★ - 스타일 설정/반환
40/89
목록
 홈  PC버전 로그인 일본어
그누앞단언어
그누뒷단언어
그외코딩언어
그누보드
제작의뢰
Q&A
커뮤니티 2
웹유틸
회원센터
홈짱닷컴 PC버전 로그인