HTML DOM NodeList forEach()
❮ NodeList 对象示例
对文档的每个子节点执行一个函数
const list = document.body.childNodes;
list.forEach(
function(node, index) {
text += index + " " + node;
}
);
自己动手试一试 »
列出文档的每个子节点的名称
const list = document.body.childNodes;
list.forEach(
function(node) {
text += node.nodeName;
}
);
更多示例见下文。
自己动手试一试 »描述
forEach() 方法对 NodeList 中的每个节点执行一个回调函数。
另请参阅
语法
nodelist.forEach(function(currentValue, index, arr), thisValue)
参数
| function() | 必需。 为每个节点运行的函数。 |
| currentValue | 必需。 当前节点的值。 |
| index | 可选。 当前节点的索引。 |
| arr | 可选。 当前节点的 NodeList。 |
| thisValue | 可选。默认 undefined。作为其 this 值传递给函数的值。 |
返回值
| 无 |
更多示例
示例
列出文档的每个子节点的类型
const list = document.body.childNodes;
list.forEach(
function(node) {
text += node.nodeType;
}
);
自己动手试一试 »
浏览器支持
nodelist.forEach() 是 DOM Level 4 (2015) 功能。
所有现代浏览器都支持它
| Chrome | Edge | Firefox | Safari | Opera |
| 是 | 是 | 是 | 是 | 是 |
nodelist.forEach() 在 Internet Explorer 11 (或更早版本) 中不支持。
❮ NodeList 对象