ParentNode.children
Node.children
唯讀屬性會回傳一個 Node
之子元素
的動態(live)HTMLCollection
。
語法
var children = node.children;
children
是一個 HTMLCollection
,為一個有順序性、由 node
中的 DOM 子元素所組成的集合。假如沒有子元素,則 children
內便不包含任何元素,且 length
為 0
。
範例
var foo = document.getElementById('foo');
for (var i = 0; i < foo.children.length; i++) {
console.log(foo.children[i].tagName);
}
Polyfill
// Overwrites native 'children' prototype.
// Adds Document & DocumentFragment support for IE9 & Safari.
// Returns array instead of HTMLCollection.
;(function(constructor) {
if (constructor &&
constructor.prototype &&
constructor.prototype.children == null) {
Object.defineProperty(constructor.prototype, 'children', {
get: function() {
var i = 0, node, nodes = this.childNodes, children = [];
while (node = nodes[i++]) {
if (node.nodeType === 1) {
children.push(node);
}
}
return children;
}
});
}
})(window.Node || window.Element);
規範
Specification | Status | Comment |
---|---|---|
DOM The definition of 'ParentNode.children' in that specification. |
Living Standard | Initial definition. |
瀏覽器相容性
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! (en-US)
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Edge | Opera | Safari |
---|---|---|---|---|---|---|
Basic support (on Element ) |
1.0 | 3.5 (1.9.1) | 9.0 [1] | 38.0 | 10.0 | 4.0 |
Support on Document and DocumentFragment
Experimental
|
29.0 | 25.0 (25.0) | No support | No support | 16.0 | No support |
Support on SVGElement (en-US) |
(Yes) | (Yes) | No support | No support | ? | No support |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support (on Element ) |
(Yes) | 1.0 (1.9.1) | (Yes) | (Yes) | (Yes) |
Support on Document and DocumentFragment
Experimental
|
(Yes) | 25.0 (25.0) | No support | 16.0 | No support |
[1] Internet Explorer 6, 7 and 8 supported it, but erroneously includes Comment
(en-US) nodes.
參見
- The
ParentNode
andChildNode
interfaces.