My TIPs for the programing

プログラミング学習記録

小ノードの取得

タグ指定による子ノードの取得は、他の親ノードにまで範囲を持つから、以下の方法を用いると良いでしょう。

 

子ノードを取得

parent.childNodes // 子ノードをすべて取得
parent.firstChild // 一番最初の子ノードを取得
parent.lastChild // 一番末尾の子ノードを取得
// parent: 親要素

 

(コード例)

<script>
const lists = document.getElementById("lists")

console.log("すべての子ノード",lists.childNodes);
console.log("最初の子ノード",lists.firstChild);
console.log("末尾の子ノード",lists.lastChild);

</script>


TIPs

・.firstchildの方が.lastchildよりも処理速度が早い。