NodeList

このページは翻訳中です。
翻訳作業に参加する場合は、履歴にある翻訳者と連絡·調整してください。

Summary

NodeList objects are collections of nodes returned by getElementsByTagName, getElementsByTagNameNS, Node.childNodes, querySelectorAll, getElementsByClassName, etc.

Properties

length

Reflects the number of elements in the NodeList. 

Methods

item ( idx )
Returns an item in the list by its index, or null if out-of-bounds. Equivalent to nodeList[idx]

Description

A "live" collection

In most cases, the NodeList is a live collection. This means that changes on the DOM tree are going to be reflected on the collection.

var links = document.getElementsByTagName('a');
// links.length === 2 for instance.

document.body.appendChild( links[0].cloneNode(true) ); // another link is added to the document
// the 'links' NodeList is automatically updated
// links.length === 3 now.

If the NodeList is the return value of document.querySelectorAll, it is NOT live.

Why can't I use forEach or map on a NodeList?

NodeList are used very much like arrays and it would be tempting to use Array.prototype methods on them. This is, however, impossible.

JavaScript has an inheritance mechanism based on prototypes. Array instances inherit array methods (such as forEach or map) because their prototype chain looks like the following:

myArray --> Array.prototype --> Object.prototype --> null (the prototype chain of an object can be obtained by calling several times Object.getPrototypeOf)

forEach, map and the likes are own properties of the Array.prototype object.

Unlike arrays, NodeList prototype chain looks like the following:

myNodeList --> NodeList.prototype --> Object.prototype --> null

NodeList.prototype contains the item method, but none of the Array.prototype methods, so they cannot be

Workarounds

One idea would be to add Array.prototype methods to NodeList.prototype. However, be aware that Extending the DOM is dangerous, especially in old version of Internet Explorer (6, 7, 8).

for(prop in Array.prototype){
  if(Array.prototype.hasOwnProperty(prop) && typeof(Array.prototype[prop]) === 'function')
    NodeList[prop] = Array.prototype[prop];
}

var links = document.getElementsByTagName('a');
links.forEach(function(link){ // works in some browsers, throws in others
  link.style.color = '#0F0';
});

Another approach without extending the DOM:

var forEach = Array.prototype.forEach;

var links = document.getElementsByTagName('a');
forEach.call(links, function(link){ // works in most browsers (please document here if you find browsers where it doesn't)
  link.style.color = '#0F0';
});

Example

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 for...in or for each...in 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.

Specification

Document Tags and Contributors

Contributors to this page: ethertank
最終更新者: ethertank,