ParentNode
の children
プロパティは、呼び出された際のノードの子要素ノードをすべて含んだ動的な(生きている) HTMLCollection
を返す、読み取り専用プロパティです。
構文
let children = node.children;
値
node
の子の DOM要素の生きている順序付きコレクションの、 HTMLCollection
です。コレクションの item()
メソッドか、JavaScript の配列スタイルの記法を使って、コレクション内の個々の子ノードにアクセスすることができます。
ノードが子要素を持たない場合、 children
は要素を含まず、length
は 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 ParentNode.children の定義 |
現行の標準 | 初めての定義 |
ブラウザー実装状況
BCD tables only load in the browser
このページの互換表は構造化データで生成されました。このデータに貢献したい方は、https://github.com/mdn/browser-compat-data をチェックし、プルリクエストを送信してください。
関連情報
ParentNode
およびChildNode
インターフェイス