Danh sách nút HTML DOM choEach()
❮ Đối tượng NodeListVí dụ
Thực thi một chức năng cho từng nút con của tài liệu:
const list = document.body.childNodes;
list.forEach(
function(node, index) {
text += index + " " + node;
}
);
Hãy tự mình thử »Liệt kê tên các nút con của tài liệu:
const list = document.body.childNodes;
list.forEach(
function(node) {
text += node.nodeName;
}
);
Thêm ví dụ dưới đây.
Hãy tự mình thử »Sự miêu tả
Phương thức forEach()
thực thi chức năng gọi lại cho mỗi nút trong NodeList .
Xem thêm:
Cú pháp
nodelist .forEach( function(currentValue, index, arr), thisValue )
Thông số
function() | Required. A function to run for each node. |
currentValue | Required. The value of the current node. |
index | Optional. The index of the current node. |
arr | Optional. The NodeList of the current node. |
thisValue | Optional. Default undefined .A value passed to the function as its this value. |
Giá trị trả về
KHÔNG CÓ |
Thêm ví dụ
Ví dụ
Liệt kê các loại nút con của tài liệu:
const list = document.body.childNodes;
list.forEach(
function(node) {
text += node.nodeType;
}
);
Hãy tự mình thử »Hỗ trợ trình duyệt
nodelist.forEach()
là tính năng DOM Cấp 4 (2015).
Nó được hỗ trợ trong tất cả các trình duyệt hiện đại:
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
nodelist.forEach()
không được hỗ trợ trong Internet Explorer 11 (hoặc phiên bản cũ hơn).
❮ Đối tượng NodeList