克隆&单独删除递增div而不删除第一个div

时间:2014-02-24 19:55:08

标签: jquery

我正在使用查询来克隆div,但是当删除它们时,我希望第一个到位并且不被删除,不能让我的头围绕它。

这是jquery

function updateClonedInput(index, element) {
$(element).appendTo("body").attr("id", "clonedInput" +  index);
$(element).find(">:first-child").attr("id", "show_upload_image_link_" + index);
$(element).find(">:first-child").attr("name", "kandibox_theme_hero_options[show_upload_image_link_" + index + "]");
$(element).find(">:first-child").attr("value", "<?php echo $hero_options['show_upload_image_link_" + index + "']; ?>");
$(element).find(">:first-child").next().attr("id", "show_upload_image_link_button_" + index);
}

$(document).on("click", "button.clone", function(){
var cloneIndex = $(".clonedInput").length + 1;
var new_Input = $(this).parents(".clonedInput").clone();
updateClonedInput(cloneIndex, new_Input);    
});
$(document).on("click", "button.remove", function(){
$(this).parents(".clonedInput").remove();

$(".clonedInput").each( function (cloneIndex, clonedElement) {
    updateClonedInput(cloneIndex + 1, clonedElement);
})
});

这是我工作代码的一小部分。

http://jsfiddle.net/vs8p5/13/

1 个答案:

答案 0 :(得分:0)

据我所知(今天咖啡的简称)你想要克隆元素然后销毁元素,但你也要销毁/删除原文。

我也有这个问题。这个问题可以通过创建一个隐藏的基本元素集来解决,当你克隆它们时,一定要更改克隆元素的选择器(id或class)名称。

当您销毁克隆元素时,不会破坏隐藏元素和基本原始元素。同样通过这种方式,您可以创建单独的可寻址克隆,这些克隆也可以进一步修改或克隆。

始终将基本元素保存在浏览器中。这当然不是唯一的方法,而只是一个建议。我经常在窗口的数据中存储较大的“数据”,这些数据在所有“浏览源”中都不可见,但这超出了您的问题的范围。

请记住,克隆后,更改克隆的.show();