Copy the content of a div into another div

前端 未结 4 1368
野性不改
野性不改 2020-12-09 19:01

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. Thi

相关标签:
4条回答
  • 2020-12-09 19:07

    Alternative in jquery You can use clone and put it in your target div. Example:

    $('#mydiv1').clone().appendTo('#mydiv2');
    

    Copy #mydiv1 into #mydiv2

    0 讨论(0)
  • 2020-12-09 19:12
    var divs = document.getElementById('mydiv1').innerHTML;
    
    document.getElementById('mydiv2').innerHTML= divs;
    
    0 讨论(0)
  • 2020-12-09 19:22

    You can use the .ready() event of jquery

    jQuery(document).ready(function() {
        jQuery('.div1').html(jQuery("#div2").html());
    }
    

    Also a working DEMO.

    0 讨论(0)
  • 2020-12-09 19:30

    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;
    

    DEMO http://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>
    
    0 讨论(0)
提交回复
热议问题