Execute a function for each of the document's child nodes:
const list = document.body.childNodes;
list.forEach(
function(node, index) {
text += index + " " + node;
}
);
Try it Yourself »
List the names of the document's child nodes:
const list = document.body.childNodes;
list.forEach(
function(node) {
text += node.nodeName;
}
);
More examples below.
Try it Yourself »The forEach()
method executes a callback function for each node in a NodeList.
nodelist.forEach(
function(currentValue, index, arr), thisValue)
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. |
NONE |
List the types of the document's child nodes:
const list = document.body.childNodes;
list.forEach(
function(node) {
text += node.nodeType;
}
);
Try it Yourself »
nodelist.forEach()
is a DOM Level 4 (2015) feature.
It is supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
nodelist.forEach()
is not supported in Internet Explorer 11 (or earlier).
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!