how to clone content of a div to another div

前端 未结 4 1974
时光说笑
时光说笑 2021-02-01 03:08

I want to copy the content of a selected div to another div with jquery clone. but I dont want to append it anywhere

what I mean is when we make a clone of a div with j

4条回答
  •  旧巷少年郎
    2021-02-01 03:47

    $(".from").click(function () {
         $(".from").removeClass("CloneMe");
         $("#to").html('');
         $(this).addClass("CloneMe");
         $(".CloneMe").clone().appendTo("#to");
    
    });
    

    You can add a class on click (or other event) that is the hard coded to clone. In this example there is a list of same class names containing styled content (divs etc) - add the .CloneMe class but first remove that class to empty the div in case the user selects a different item. )to be safe remove any html as well. Then apply the class using (this) to avoid grabbing all of the items with that class name and finally append to the div. The result is the user can select any item with that class name and populate it in the container. - I imagine using a class for the container would allow you to populate it in more than one place.


提交回复
热议问题