Clone
and change id

前端 未结 4 1914
伪装坚强ぢ
伪装坚强ぢ 2020-12-01 06:30

How can I using javascript make clone of some

and set his id different from original. Jquery also will be nice.

相关标签:
4条回答
  • 2020-12-01 06:36

    jQuery have method clone()

    var original = $("#original");
    var newClone = original.clone();
    
    0 讨论(0)
  • 2020-12-01 06:42

    Use it:

    JQuery

    var clonedDiv = $('#yourDivId').clone();
    clonedDiv.attr("id", "newId");
    $('#yourDivId').after(cloneDiv);
    
    0 讨论(0)
  • 2020-12-01 06:52

    I had the same problem. I've solved that by setting a counter and appending it to the ID. Hope it helps you too:

    <script type="text/javascript">
         var counter = 1; //Set counter
    
    function clone(sender, eventArgs) {
         var $row = $('#yourID');
    
         var $clone = $row.clone(); //Making the clone                      
         counter++; // +1 counter
    
         //Change the id of the cloned elements, append the counter onto the ID 
         $clone.find('[id]').each(function () { this.id += counter });
    }
    </script>
    
    0 讨论(0)
  • 2020-12-01 06:55
    var div = document.getElementById('div_id'),
        clone = div.cloneNode(true); // true means clone all childNodes and all event handlers
    clone.id = "some_id";
    document.body.appendChild(clone);
    
    0 讨论(0)
提交回复
热议问题