最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to insert a div as the first element in a container with JavaScript - Stack Overflow

programmeradmin4浏览0评论

I have created a div with Javascript, this displays correctly, however it is not in the place where I want, I would like to put it inside a container as the first element. I'm not very good, I'm trying to learn, so sorry for the triviality of the question.

How do I put the div I created inside an already existing container as the first element?

Beyond that I would like to understand the logic of the operation, for example, how can I move the new div as the last element? Or as a second element ?

This is the Js code

// Add New Element
var newEl = document.createElement("div");
var text = document.createTextNode("Hello");
   newEl.appendChild(text);
   var element = document.getElementById("main_container");
   element.appendChild(newEl);

This is what I am trying to achieve

<div id="main_container" class="something">
   <div class="new_element">Hello</div>
   <div class="item">One</div>
   <div class="item">Two</div>
   <div class="item">Three</div>
</div>

This is what I got for now

<div id="main_container" class="something">
   <div class="item">One</div>
   <div class="item">Two</div>
   <div class="item">Three</div>
</div>
<div>Hello</div>

I have created a div with Javascript, this displays correctly, however it is not in the place where I want, I would like to put it inside a container as the first element. I'm not very good, I'm trying to learn, so sorry for the triviality of the question.

How do I put the div I created inside an already existing container as the first element?

Beyond that I would like to understand the logic of the operation, for example, how can I move the new div as the last element? Or as a second element ?

This is the Js code

// Add New Element
var newEl = document.createElement("div");
var text = document.createTextNode("Hello");
   newEl.appendChild(text);
   var element = document.getElementById("main_container");
   element.appendChild(newEl);

This is what I am trying to achieve

<div id="main_container" class="something">
   <div class="new_element">Hello</div>
   <div class="item">One</div>
   <div class="item">Two</div>
   <div class="item">Three</div>
</div>

This is what I got for now

<div id="main_container" class="something">
   <div class="item">One</div>
   <div class="item">Two</div>
   <div class="item">Three</div>
</div>
<div>Hello</div>
Share Improve this question edited Aug 11, 2022 at 20:07 Snorlax asked Aug 11, 2022 at 19:00 SnorlaxSnorlax 2932 gold badges14 silver badges50 bronze badges 1
  • 2 Look into Node.insertBefore() – mykaf Commented Aug 11, 2022 at 19:03
Add a ment  | 

2 Answers 2

Reset to default 5

This should work:

element.insertBefore(newEl, element.firstChild)
const parent;
const newFirstChild;
parent.insertBefore(newFirstChild, parent.firstChild);

In your case:

element.insertBefore(newEl, element.firstChild)

If you want to insert at a different index you can do it like this:

parent.insertBefore(newEl, parent.children[2])

Codepen

发布评论

评论列表(0)

  1. 暂无评论