Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

html - How reorganize a group of div , pure JavaScript

Hello I'm a beginner and I'm trying to manipulate the DOM by moving existing divs . How to make "for" loops on ". appendChild" in pure javaScript, no Jquery.

I know how to manipulate ".appendChild" just by the id, in this particular case it is a group of divs that I have to reorganize, I know that it is necessary to make a loop, but how to move a child div into a parent and vice versa by making loops, the is my question?

Can you help me please, thank you for your help.

<!-- ===============
my current HTML 
=============== -->

<div
  id="elemen1"
  class="gt_swatches--select"
  data-name="dog"
  data-value="diva"
>
</div>
<a onclick="Selec()" class="Enb1"></a>
 <span>diva</span>
 <div></div>

<div
  id="elemen2"
  class="gt_swatches--select"
  data-name="dog"
  data-value="divb"
>
</div>
<a onclick="Selec()" class="Enb1"></a>
 <span>divc</span>
 <div></div>

<div
  id="elemen3"
  class="gt_swatches--select"
  data-name="dog"
  data-value="divc"
>
</div>
<a onclick="Selec()" class="Enb1"></a>
 <span>divc</span>
 <div></div>

<!-- ==================
The desired result
================== -->

<a onclick="Selec()" class="Enb1">
 <div
  id="elemen1"
  class="gt_swatches--select"
  data-name="dog"
  data-value="diva"
>
<div id="NewId1" >
<span>diva</span>
</div>
</div>
</a>
 
<a onclick="Selec()" class="Enb1">
 <div
  id="elemen2"
  class="gt_swatches--select"
  data-name="dog"
  data-value="divb"
>
<div id="NewId2" >
<span>divb</span>
</div>
</div>
</a>
 
<a onclick="Selec()" class="Enb1">
 <div
  id="elemen3"
  class="gt_swatches--select"
  data-name="dog"
  data-value="divc"
>
<div id="NewId3" >
<span>divc</span>
</div>
</div>
</a>
question from:https://stackoverflow.com/questions/65842943/how-reorganize-a-group-of-div-pure-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...