简体   繁体   中英

How do you append an element but also keep it in it's original position?

I'm looking for a way to copy the exact code of an element and move it to another element while keeping it in it's original place also.

I have some JS where basically you click on a picture, the picture will then be appended to a box. However, the picture completely moves. I want the picture to stay as well as be moved to the box as I am looking to grey it out instead of make it disappear.

I don't need specific answers, just a general javascript answer on how I can achieve this, thank you.

您可以通过创建克隆来实现此目的。

$('#div').append($('#image').clone())

Since you are using jQuery have you tried clone() ? - straight from the docs..

$( ".hello" ).clone().appendTo( ".goodbye" );

http://api.jquery.com/clone/

You can get the html of the image and then copy it to the image container:

javascript

function addImageTo(element){
    var oldHTML = element.innerHTML;
    var appendHTML = document.getElementById('yourImage').innerHTML;

    element.innerHTML = oldHTML + appendHTML;
}

JQuery

function addImageTo(element){
    element.append($('#yourImage').innerHTML);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM