--- title: NodeList slug: Web/API/NodeList translation_of: Web/API/NodeList ---
NodeList
物件是節點的集合,可藉由 {{domxref("Node.childNodes")}} 屬性以及 {{domxref("document.querySelectorAll()")}} 方法取得。
雖然 NodeList
不是 Array
,但仍可以使用 forEach()
方法來進行迭代。一些老舊瀏覽器並未實作此方法。
在某些情況下,NodeList
為動態集合(live collection),意思是 DOM 的改變會反映於集合。例如,{{domxref("Node.childNodes")}} 便是即時更新(live)的:
var parent = document.getElementById('parent'); var child_nodes = parent.childNodes; console.log(child_nodes.length); // let's assume "2" parent.appendChild(document.createElement('div')); console.log(child_nodes.length); // should output "3"
在其他的情況下,NodeList
是一個靜態的集合(static collection),代表任何之後的 DOM 變化都不會影響集合的內容。{{domxref("document.querySelectorAll()")}} 會回傳一個靜態的 NodeList
。
當你要選擇如何迭代 NodeList
中的項目時,最好能於心中區分動態與靜態集合,尤其是在取得集合長度(length of the list)的時候。
NodeList
.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).NodeList
element.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 {{domxref("element")}} objects. Also, for..in
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, {{domxref("NodeList.forEach()", "forEach()")}}, as well as {{domxref("NodeList.entries()", "entries()")}}, {{domxref("NodeList.values()", "values()")}}, and {{domxref("NodeList.keys()", "keys()")}}
There is also an Internet Explorer compatible way to use {{jsxref("Array.forEach()", "Array.prototype.forEach")}} for iteration.
var list = document.querySelectorAll( 'input[type=checkbox]' ); Array.prototype.forEach.call(list, function (item) { item.checked = true; });
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); //or 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()
Specification | Status | Comment |
---|---|---|
{{SpecName('DOM WHATWG', '#interface-nodelist', 'NodeList')}} | {{ Spec2('DOM WHATWG') }} | |
{{SpecName('DOM4', '#interface-nodelist', 'NodeList')}} | {{ Spec2('DOM4') }} | |
{{SpecName('DOM3 Core', 'core.html#ID-536297177', 'NodeList')}} | {{ Spec2('DOM3 Core') }} | |
{{SpecName('DOM2 Core', 'core.html#ID-536297177', 'NodeList')}} | {{ Spec2('DOM2 Core') }} | |
{{SpecName('DOM1', 'level-one-core.html#ID-536297177', 'NodeList')}} | {{ Spec2('DOM1') }} | Initial definition. |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
entries() , keys() , values() , forEach() |
{{CompatChrome(51)}} | {{CompatNo}} | {{CompatGeckoDesktop(50)}} | {{CompatNo}} | 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 | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
entries() , keys() , values() , forEach() |
{{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatGeckoMobile(50)}} | {{CompatUnknown}} | {{CompatNo}} | {{CompatUnknown}} | 10 (maybe prior) | {{CompatChrome(51)}} |