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

javascript - js - duplicate all child nodes - Stack Overflow

programmeradmin5浏览0评论

I want to duplicate all child nodes in a container, and insert them in the same container.

For example, I have:

<div id="container">
  <div class="square red"></div>
  <div class="square green"></div>
</div>

And after running this JS:

function duplicateChildNodes (parentId){
  var parent = document.getElementById(parentId);
  NodeList.prototype.forEach = Array.prototype.forEach;
  var children = parent.childNodes;
  children.forEach(function(item){
    parent.appendChild(item);
  });
};

duplicateChildNodes("container");

I should have:

<div id="container">
  <div class="square red"></div>
  <div class="square green"></div>
  <div class="square red"></div>
  <div class="square green"></div>
</div>

For some reason, it doesen't work. How do I solve it ?
A pen:

I want to duplicate all child nodes in a container, and insert them in the same container.

For example, I have:

<div id="container">
  <div class="square red"></div>
  <div class="square green"></div>
</div>

And after running this JS:

function duplicateChildNodes (parentId){
  var parent = document.getElementById(parentId);
  NodeList.prototype.forEach = Array.prototype.forEach;
  var children = parent.childNodes;
  children.forEach(function(item){
    parent.appendChild(item);
  });
};

duplicateChildNodes("container");

I should have:

<div id="container">
  <div class="square red"></div>
  <div class="square green"></div>
  <div class="square red"></div>
  <div class="square green"></div>
</div>

For some reason, it doesen't work. How do I solve it ?
A pen: https://codepen.io/t411tocreate/pen/gXqYWj

Share Improve this question asked Dec 1, 2017 at 21:27 t411tocreatet411tocreate 4712 gold badges5 silver badges16 bronze badges 1
  • that dom element only exists in one place so after append it is moved to a new place, you need clone node first. ex: var cln = itm.cloneNode(true); – Dalin Huang Commented Dec 1, 2017 at 21:30
Add a ment  | 

1 Answer 1

Reset to default 13

that dom element only exists in one place so after append it is moved to a new place (in your case it is removed and appended to the same place)

You need clone node first. ex: var cln = itm.cloneNode(true);

check the updated example below:

function duplicateChildNodes (parentId){
  var parent = document.getElementById(parentId);
  NodeList.prototype.forEach = Array.prototype.forEach;
  var children = parent.childNodes;
  children.forEach(function(item){
    var cln = item.cloneNode(true);
    parent.appendChild(cln);
  });
};

duplicateChildNodes("container");
#container{
  border: 1px solid #ccc;
  padding: 2px;
}
.square{
  width: 100px;
  height: 100px;
  display: block;
  margin: 2px;
}
.red{
  background: red;
}

.green{
  background: green;
}
<div id="container">
  <div class="square red"></div>
  <div class="square green"></div>
</div>

发布评论

评论列表(0)

  1. 暂无评论