HTML DOM NodeList length 属性

定义和用法

length 属性返回 NodeList 中的节点数。

length 属性是只读的。

实例

例子 1

获取文档中子节点的数量:

const nodeList = document.body.childNodes;
let number = nodeList.length;

직접 시도해보세요

例子 2

获取 <body> 元素的子节点:

const nodeList = document.body.childNodes;

직접 시도해보세요

예제 3

"myDIV"에 있는 자식 노드 수를 가져옵니다:

const element = document.getElementById("myDIV");
let numb = element.childNodes.length;

직접 시도해보세요

예제 4

"myDIV"에 <p> 요소의 수:

const div = document.getElementById("myDIV");
const list = div.querySelectorAll("p");
let number = list.length;

직접 시도해보세요

예제 5

"myDIV"에 있는 모든 <p> 요소를 순회하며 그들의 글꼴 크기를 변경합니다:

const div = document.getElementById("myDIV");
const list = div.querySelectorAll("p");
for (let i = 0; i < list.length; i++) {
  list[i].style.fontSize = "red";
}

직접 시도해보세요

예제 6

모든 자식 노드를 순회하며 각 노드의 이름을 수집합니다:

const list = document.body.childNodes;
let text = "";
for (let i = 0; i < list.length; i++) {
  text += list[i].nodeName + "<br>";
}

직접 시도해보세요

문법

nodelist.length

반환 값

형식 설명
숫자 NodeList에 있는 노드 수.

브라우저 지원

nodelist.length는 DOM Level 1 (1998) 기능입니다.

모든 현대 브라우저가 지원합니다:

크롬 IE 에지 파이어폭스 사파리 오페라
크롬 IE 에지 파이어폭스 사파리 오페라
지원 9-11 지원 지원 지원 지원

관련 페이지

entries() 메서드

forEach() 메서드

item() 메서드

keys() 메서드

values() 메서드

NodeList 객체

childNodes() 메서드

querySelectorAll() 메서드

getElementsByName() 메서드