通常の JavaScript で先頭と末尾の追加を実装するにはどうすればよいでしょうか? 質問する

通常の JavaScript で先頭と末尾の追加を実装するにはどうすればよいでしょうか? 質問する

どのように実装すればよいですか先頭に追加するそして追加jQuery を使用せずに通常の JavaScript で実行できますか?

ベストアンサー1

始めるためのスニペットを以下に示します。

theParent = document.getElementById("theParent");
theKid = document.createElement("div");
theKid.innerHTML = 'Are we there yet?';

// append theKid to the end of theParent
theParent.appendChild(theKid);

// prepend theKid to the beginning of theParent
theParent.insertBefore(theKid, theParent.firstChild);

theParent.firstChild内部の最初の要素への参照が提供されtheParenttheKidその前に配置されます。

おすすめ記事