如何用另一个副本替换div?

时间:2012-12-05 06:02:55

标签: jquery html replace

function switch(){
    $('#get1').clone();
    $('#get2').replaceWith($('#get1'));
};

我在此预计#get1的一个副本会保留在原始位置,但会发生什么:它已经消失,因为没有克隆。
所以,我希望#get2替换为#get1的COPY,而不是原始的。

2 个答案:

答案 0 :(得分:2)

您是creating clone but not using clone,而不是使用原始对象。您必须将clone对象分配给某个对象,并在replaceWith函数中使用它

function switch(){
    yourClone = $('#get1').clone();
    $('#get2').replaceWith(yourClone );
};

答案 1 :(得分:2)

首先,不要因为保留字而使用'switch'作为变量名。

无论如何,这是我的答案

function doSwitch(){
    var $get1 = $('#get1').clone();
    $('#get2').replaceWith($get1);
}

将克隆对象设置为要使用的变量。