本文介绍了将div的内容复制到另一个div中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有两个div。他们都拥有相同的css属性,但其中一个拥有24个其他div。我希望所有这些24个div都被复制到另一个div中。
这就是它的样子:

 < div id =mydiv1> 
< div id =div1>
< / div>
< / div id =div2>
< / div>
//并一直到div 24.
< / div>

< div id =mydiv2>
< / div>

p>我希望当页面加载时,将mydiv1中的所有24个div复制到mydiv2中。



预先致谢

解决方案

首先,我们将div分配给变量(可选)

  var firstDivContent = document.getElementById('mydiv1'); 
var secondDivContent = document.getElementById('mydiv2');

现在只需将mydiv1的内容分配给mydiv2。

  secondDivContent.innerHTML = firstDivContent.innerHTML; 



DEMO



完整代码

 < html> 
< head>
<脚本类型= 文本/ JavaScript的 >
函数copyDiv(){
var firstDivContent = document.getElementById('mydiv1');
var secondDivContent = document.getElementById('mydiv2');
secondDivContent.innerHTML = firstDivContent.innerHTML;
}
< / script>
< / head>
< body onload =copyDiv();>
< div id =mydiv1>
< div id =div1>
< / div>
< div id =div2>
< / div>
< / div>

< div id =mydiv2>
< / div>
< / body>
< / html>


I have two divs on my website. They both share the same css properties, but one of them holds 24 other divs. I want all of these 24 divs to be copied into the other div.This is how it looks like:

<div id="mydiv1">
    <div id="div1">
    </div>
    </div id="div2>
    </div>
    //and all the way up to div 24.
</div>

<div id="mydiv2">
</div>

I want all of the 24 divs within mydiv1 to be copied into mydiv2 when the page loads.

Thanks in advance

解决方案

Firstly we are assigning divs into variables (optional)

var firstDivContent = document.getElementById('mydiv1');
var secondDivContent = document.getElementById('mydiv2');

Now just assign mydiv1's content to mydiv2.

secondDivContent.innerHTML = firstDivContent.innerHTML;

DEMOhttp://jsfiddle.net/GCn8j/

COMPLETE CODE

<html>
<head>
  <script type="text/javascript">
    function copyDiv(){
      var firstDivContent = document.getElementById('mydiv1');
      var secondDivContent = document.getElementById('mydiv2');
      secondDivContent.innerHTML = firstDivContent.innerHTML;
    }
  </script>
</head>
<body onload="copyDiv();">
  <div id="mydiv1">
      <div id="div1">
      </div>
      <div id="div2">
      </div>
  </div>

  <div id="mydiv2">
  </div>
</body>
</html>

这篇关于将div的内容复制到另一个div中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-19 18:41