0
我正在使用jquery 1.10.2並試圖克隆一個div,我已經成功完成了,但我也需要改變該div的id和它的所有孩子,我目前沒有成功。jquery clone div改變它的id和和所有的孩子的id
我有搜索這個,並找到幾個例子來幫助例如this和this,但他們沒有工作。我真的不知道爲什麼。我知道我一定在做一些非常明顯的錯誤。什麼是我沒有工作的原因? (我已經離開了我的嘗試 - 但發表了評論)
$(document).ready(function() {
$("#btnSecondContact").click(function() {
$("#divCustomerData").removeClass("col-lg-6 col-md-6");
$("#divCustomerAddr").removeClass("col-lg-6 col-md-6");
$("#divCustomerData").addClass("col-lg-4 col-md-4");
$("#divCustomerAddr").addClass("col-lg-4 col-md-4");
$('#divCustomerData').after(
$('#divCustomerData').clone()
//$('#divCustomerData').clone().attr("id", "newId").find("#divCustomerData").attr("id", "#divCustomerData_cloned")
//$('#divCustomerData').clone(function() {
// $(this).attr("id", $(this).attr("id") + "_cloned")
//})
)
//$("#divCustomerData").clone(false).find("*[id]").andSelf().each(function() { $(this).attr("id", $(this).attr("id") + "_cloned"); });
$("#btnSecondContact").prop('disabled', true);
})
});
感謝您的快速響應NSE。但嘗試這不起作用。然而,如果我只是使用$ clon,那麼我會得到克隆的元素,但很明顯,我仍然遇到id不會更改的問題 – tony09uk