克隆一个div两次

时间:2012-01-26 18:02:43

标签: jquery clone

我基本上试图克隆已经存在于页面上的div,两次。这就是我要克隆的内容:

<div class="tnt-zombies">
     //code in here
</div>

我还需要在第二个克隆上有一个增强的克隆div和一类优质。我还想在这两个克隆中添加一个克隆类,所以当我去删除它们时,我可以调用克隆类。

我希望得到的最终结果是:

<div class="tnt-zombies">//other code in here</div>
<div class="tnt-zombies cloned enhanced">//other code in here</div>
<div class="tnt-zombies cloned premium">//other code in here</div>

我希望这是有道理的。

2 个答案:

答案 0 :(得分:3)

试试这个:

var the_clone = $('.tnt-zombies').clone().addClass('cloned');

var enhanced = the_clone.clone().addClass('enhanced');
var premium = the_clone.clone().addClass('premium');

$('body').append(enhanced, premium);

小提琴:http://jsfiddle.net/maniator/Arzmm/

答案 1 :(得分:0)

您也可以一次性完成此操作:

$('.tnt-zombies').clone().addClass('cloned enhanced').insertAfter('.tnt-zombies').end().clone().addClass('cloned premium').insertAfter('.tnt-zombies:last');