ParentNode property children is a read-only property that returns a live HTMLCollection which contains all of the child 元素 of the node upon which it was called.

句法

let children = node.children;
					

HTMLCollection which is a live, ordered collection of the DOM elements which are children of node . You can access the individual child nodes in the collection by using either the item() method on the collection, or by using JavaScript array-style notation.

If the node has no element children, then children is an empty list with a length of 0 .

范例

const foo = document.getElementById('foo');
for (let 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() {
        let i = 0, node, nodes = this.childNodes, children = [];
        while (node = nodes[i++]) {
          if (node.nodeType === 1) {
            children.push(node);
          }
        }
        return children;
      }
    });
  }
})(window.Node || window.Element);
					

规范

规范 状态 注释
DOM
The definition of 'ParentNode.children' in that specification.
实时标准 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
children Chrome 1 Edge 12 Firefox 3.5 IE 9
9
Internet Explorer 6, 7 and 8 supported it, but erroneously includes 注释 节点。
Opera 10 Safari 4 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 9 Samsung Internet Android 1.0
Support on Document and DocumentFragment Chrome 29 Edge 16 Firefox 25 IE No Opera 16 Safari 9 WebView Android Yes Chrome Android Yes Firefox Android ? Opera Android ? Safari iOS 9 Samsung Internet Android Yes
Support on SVGElement Chrome Yes Edge 16 Firefox Yes IE No Opera ? Safari 9 WebView Android Yes Chrome Android Yes Firefox Android ? Opera Android ? Safari iOS 9 Samsung Internet Android Yes

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

见实现注意事项。

另请参阅

元数据

  • 最后修改: