0
我想克隆和重命名ID和名稱,但下面的代碼似乎並沒有工作。如果有任何建議,請讓我知道嗎?克隆和重命名ID和名稱
它看起來像下面的行有問題。
cloned.id = what.attr('id')+「_」+ cur_num;
這是我的功能。
var cur_num = 1; // Counter
$("#btnClone").click(function(){
var what = $("#MainConfig");
var where = $("#smallConfig");
var cloned = what.clone(true, true).get(0);
++cur_num;
cloned.id = what.attr('id') + "_" + cur_num; // Change the div itself.
$(cloned).find("*").each(function(index, element) { // And all inner elements.
if(element.id)
{
var matches = element.id.match(/(.+)_\d+/);
if(matches && matches.length >= 2)
element.id = matches[1] + "_" + cur_num;
}
if(element.name)
{
var matches = element.name.match(/(.+)_\d+/);
if(matches && matches.length >= 2)
element.name = matches[1] + "_" + cur_num;
}
});
$(cloned).appendTo(where);
});
});
你似乎對jQuery集合對象和實際的DOM對象有些困惑。 –