NodeList: thuộc tính length

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Thuộc tính NodeList.length trả về số lượng mục trong một NodeList.

Giá trị

Một giá trị số nguyên biểu thị số lượng mục trong NodeList.

Ví dụ

Thuộc tính length thường hữu ích trong lập trình DOM. Nó thường được dùng để kiểm tra độ dài của một danh sách, nhằm xem danh sách đó có tồn tại hay không. Nó cũng thường được dùng làm biến lặp trong một vòng for, như trong ví dụ sau.

js
// All the paragraphs in the document
const items = document.getElementsByTagName("p");

// For each item in the list,
// append the entire element as a string of HTML
let gross = "";
for (let i = 0; i < items.length; i++) {
  gross += items[i].innerHTML;
}

// gross is now all the HTML for the paragraphs

Thông số kỹ thuật

Specification
DOM
# ref-for-dom-nodelist-length①

Tương thích trình duyệt