dom_dom0200_008

「子ノード」をすべて(テキスト含む)取得するには ( A ) を、「子要素」だけに限定して取得するには ( B ) を用いる。

解答

A: childNodes
B: children

解説

childNodes は全ノード、children は要素ノードに限定したコレクション(HTMLCollection)を返す。レイアウトや要素操作が目的なら children が扱いやすい。

使用例(サンプルコード)

[ js ]

const section = document.querySelector('section');
console.log(section.childNodes.length); // テキスト等含む
console.log(section.children.length);   // 要素のみ