克隆后如何删除原始元素? [英] How to remove original element after it was cloned?
本文介绍了克隆后如何删除原始元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HTML:
<div id="test">
<p class="test1">test 1</p>
<p class="test2">test 2</p>
<p class="test3">test 3</p>
<p class="test4">test 4</p>
</div>
<div class="clickdiv">click</div>
jQuery
$('.clickdiv').on('click', function(){
$('.test1').clone().appendTo('#test');
}
这将使class = "test1"
再产生一个<p>
.现在如何删除原始的第一个?
This will result one more <p>
with class = "test1"
. Now how can I remove the original one that is first one?
推荐答案
我不知道为什么不能只将元素附加到父元素,而不是克隆然后删除它.
I don't know why you can't just append the element to the parent, instead of cloning it then removing it.
反正
$('.test1').remove().clone().appendTo('#test');
演示:小提琴
如果要将与test1
关联的数据和处理程序复制到克隆,则必须使用@ra_htial进行较小的更改
If you want to copy the data and handlers associated with test1
to the clone then @ra_htial with a minor change have to be used
$('.clickdiv').on('click', function () {
var $el = $('.test1');
$el.clone(true).appendTo('#test');
$el.remove();
});
演示:小提琴
这篇关于克隆后如何删除原始元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文