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 物件