NodeList 物件是節點的集合,可藉由 Node.childNodes 屬性以及 document.querySelectorAll() 方法取得。

雖然 NodeList 不是 Array,但仍可以使用 forEach() 方法來進行迭代。一些老舊瀏覽器並未實作此方法。

在某些情況下,NodeList動態集合(live collection),意思是 DOM 的改變會反映於集合。例如,Node.childNodes 便是即時更新(live)的:

var parent = document.getElementById('parent');
var child_nodes = parent.childNodes;
console.log(child_nodes.length); // let's assume "2"
console.log(child_nodes.length); // should output "3"

在其他的情況下,NodeList 是一個靜態的集合(static collection),代表任何之後的 DOM 變化都不會影響集合的內容。document.querySelectorAll() 會回傳一個靜態的 NodeList

當你要選擇如何迭代 NodeList 中的項目時,最好能於心中區分動態與靜態集合,尤其是在取得集合長度(length of the list)的時候。


The number of nodes in the NodeList.


Returns an item in the list by its index, or null if the index is out-of-bounds; can be used as an alternative to simply accessing nodeList[idx] (which instead returns  undefined when idx is out-of-bounds).
Returns an iterator allowing to go through all key/value pairs contained in this object.
Executes a provided function once per NodeList element.
Returns an iterator allowing to go through all keys of the key/value pairs contained in this object.
Returns an iterator allowing to go through all values of the key/value pairs contained in this object.


It's possible to loop over the items in a NodeList using:

for (var i = 0; i < myNodeList.length; ++i) {
  var item = myNodeList[i];  // Calling myNodeList.item(i) isn't necessary in JavaScript

Don't be tempted to use or for to enumerate the items in the list, since that will also enumerate the length and item properties of the NodeList and cause errors if your script assumes it only has to deal with element objects. Also, is not guaranteed to visit the properties in any particular order.

for...of loops will loop over NodeList objects correctly:

var list = document.querySelectorAll( 'input[type=checkbox]' );
for (var item of list) {
  item.checked = true;

Recent browsers also support iterator methods, forEach(), as well as entries(), values(), and keys()

There is also an Internet Explorer compatible way to use Array.prototype.forEach for iteration.

var list = document.querySelectorAll( 'input[type=checkbox]' );, function (item) {
  item.checked = true;

NodeList 原型

You can also add prototypes to nodelist:

var elements = document.querySelectorAll(".suggestions");

NodeList.prototype.addEventListener = function(event, func) {
    this.forEach(function(content, item) {
       content.addEventListener(event, func);

function log() {
    console.log(this, " was clicked");

elements.addEventListener("click", log);
elements.addEventListener("click", function() {
    console.log(this, "  awas clicked");
// output from both will be element was clicked the element would be HTML Element

For information about forEach see Array.prototype.forEach()



We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!
Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)
entries(), keys(), values(), forEach() 51 No support 50 (50) No support 38 10 (maybe prior)
Feature Android Android Webview Edge Firefox Mobile (Gecko) Firefox OS (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)
entries(), keys(), values(), forEach() ? ? ? 50.0 (50) ? No support ? 10 (maybe prior) 51