在克隆过程中更改内部元素id
Change inner elements id during clone
我正在克隆一个div元素,点击按钮,我可以更改我正在克隆的div元素的ID值。但是有可能改变内部元素的id吗。
在下面的代码中,我在克隆时更改#selection
的Id,我需要动态更改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);
使用类.show-tick
和.children()
方法来定位元素:
clone.children('.show-tick').attr('id', 'select-' + length);
$(function() {
//on click
$(".btn-primary").on("click", 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);
clone.children('.show-tick').attr('id', 'select-' + length++);
//append clone on the end
$("#selections").append(clone);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>
我也有类似的需求,需要更改克隆及其所有子代的id。发布我的解决方案以帮助未来的某个人。我想更改克隆人所有孩子的id和名字。
$("#form").on('click', '.clone', function (e) {
e.preventDefault();
var myid = this.id; // id of section we are cloning i.e. section_1
myid = myid.split('_');
var num = 0;
// count existing sections
$('.form_section').each(function(){
num++;
});
num++; // new number for clone
var newid = 'section_'+num;
// make and change id of the clone
var clone = $( "#section_"+myid[1] ).clone().attr("id", newid);
// get clone children
clone.children().find('input,textarea,button,a,select').attr('id', function( i, val ){
var oldid = val;
var newid = val + num;
clone.find("#"+val).attr("id", newid);
clone.find("#"+newid).attr("name", newid);
});
clone.appendTo( ".sections" );
});
您需要手动更改所有孩子的ID。
要么只更改一个:
//clone first element with new id
clone = $("#selection").clone();
clone.attr("id", newId);
clone.find("#select").attr("id","whateverID");
或者使用类似的方法更改所有子项:jQuery更改所有子级的id属性
相关文章:
- 如何选择一个类而不是其内部元素
- angularJS获取指令的内部元素
- NodeJS如何更新内部元素
- jQuery点击事件处理类元素和内部元素
- 如果释放鼠标时内部元素未悬停,则防止触发“单击”的正确方法
- 如何使内部元素具有位置:绝对覆盖溢出:隐藏外部元素
- 如何使用jquery从父元素中删除文本(不删除内部元素)
- 强制内部元素溢出-x
- jquery选择器-不选择内部元素
- onmouseover在内部元素上执行
- 设置内部元素的样式
- 角度砌体指令隐藏内部元素,错误:引用错误:未定义砌体
- 窗体内部元素的 addEventListener
- 如何获得带有内部元素填充的完整车身宽度
- 单击对内部元素执行某些操作
- 使用嵌入隐藏内部元素的父范围的指令范围
- 使用 JavaScript 从 XML 文件中获取内部元素
- 根据其内部元素动态更改 CSS
- 指令的动态模板,包装器和内部元素的不同解析行为
- ExtJS门户布局问题(内部元素的自动高度/宽度)