プロパティ firstChild

プロパティ firstChild は、要素の最初の 子ノードを保持します。要素に子要素がない場合、 null が返されます。

構文

javascript要素.firstChild;

例

要素の最初のノードのテキストを取得しましょう:

html<div id="parent">テキスト<p>段落</p><!--コメント--></div>
javascriptlet parent = document.querySelector('#parent');
let text = parent.firstChild.textContent;

console.log(text);

コード実行結果:

javascript'テキスト'

関連項目

  • プロパティ firstElementChild,
    最初の要素を含む
  • プロパティ lastChild,
    最後のノードを含む
  • プロパティ childNodes,
    要素の全てのノードを含む