简体   繁体   中英

jQuery stop append removing div

I am copying a div into another div using append. However it removes the original. I tried to use clone but it seems to only work with appendTo. But appendTo breaks my layout so I have to use append with works fine.

I am wrong that clone will not work with .append and is there another way to stop the div removing?

Thanks

              $('.compareWrapper').append(htmlStr)                              

foo.appendTo(bar)
Take foo and append it to bar .

foo.append(bar)
Take bar and append it to foo

Syntactically they're different. You have to think of what's the target object and what's the destination object. So, having said that you can move ahead in one of two ways:

var $clone = $('target').clone();
$clone.appendTo('wrapper');
$('wrapper').append($clone);

Both do the same thing.

以下不起作用?

$('.compareWrapper').append($(htmlStr).clone());

I don't see any reason for .clone() not working with .append() . The code should be:

$('.compareWrapper').append($(htmlStr).clone());

Is that what you tried? From the name of your variable, I'm assuming htmlStr is a string, not a jQuery object.

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