我正在单击按钮时克隆 DIV 元素,我可以更改正在克隆的 DIV 元素的 ID 值。但是是否可以更改内部元素的 id?
在下面的代码中,我更改了 Id#selection
克隆时,我需要动态更改 id#select
.
<div id="selections">
<div class="input-group" id="selection">
<span class="input-group-addon">
<i class="icon wb-menu" aria-hidden="true"></i>
</span>
<select class="show-tick" data-plugin="select2" id="select">
<option>True</option>
<option>False</option>
</select>
</div>
</div>
<button class="btn btn-primary" type="button" style="margin-left: 30px;">
Add new selection
</button>
下面的JS
$(function() {
//on click
$("body").on("click", ".btn-primary", function() {
alert($(".input-group").length)
var
//get length of selections
length = $(".input-group").length,
//create new id
newId = "selection-" + length++,
//clone first element with new id
clone = $("#selection").clone().attr("id", newId);
//append clone on the end
$("#selections").append(clone);
});
});
是的..完全可能如下:
var clone = $("#selection").clone();
clone.attr("id", newId);
clone.find("#select").attr("id","select-"+length);
//append clone on the end
$("#selections").append(clone);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)