OP는 중복 ID를 추가하기 전에 모든 ID를 바꾸는 방법을 요구 했으므로 이와 같은 방법이 사용될 수 있습니다.
이
<div id="smallConfig">
<div id="MainConfig_1">
<ul>
<li id="red_1">red</li>
<li id="blue_1">blue</li>
</ul>
</div>
</div>
코드는 다음과 같이 될 수 복제 된 블록의 모든 자식 요소 (후손)를 찾기 위해, 그리고 자신의 ID의은을 사용하여 수정 : 당신이 이와 같은 HTML 블록에 MainConfig_1를 복제하고 싶었 가정 카운터 :
var cur_num = 1; // Counter used previously.
//...
var cloned = $("#MainConfig_" + cur_num).clone(true, true).get(0);
++cur_num;
cloned.id = "MainConfig_" + 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) // Captures start at [1].
element.id = matches[1] + "_" + cur_num;
}
});
$(cloned).appendTo($("#smallConfig"));
이 같은 새로운 HTML을 만들려면 :이 러셀의 대답을 기반으로
<div id="smallConfig">
<div id="MainConfig_1">
<ul>
<li id="red_1">red</li>
<li id="blue_1">blue</li>
</ul>
</div>
<div id="MainConfig_2">
<ul>
<li id="red_2">red</li>
<li id="blue_2">blue</li>
</ul>
</div>
</div>
@ 아담 네 일러 존재 매끈한가 도움이되지 않습니다 최선의 방법이라고 생각이 같은 문이 그렇게 할 경우
다른를 추가합니다. –