我需要一些帮助来弄清楚如何编写一些jQuery代码.
我需要动态克隆一个表onclick.但后来我需要每次更改表及其子元素的ID.因为桌子可以有很多孩子,所以很难手动完成.我需要一种方法来改变所有子(所有后代)元素的id.我将永远只是将计数器添加到id.(我知道孩子们只能访问直接的孩子,但我只是想尝试一下,如果有效的话).如果你们知道如何在jQuery或Javascript中完成,请告诉我.
<table id="table">
<tr id = "tr" >
<td id="td">
<span id="span" value="hiii">hi</span>
</td>
</tr>
</table>
<button>Clone</button>
<script>
$("button").click(function () {
var table = $("#table").clone(true,true)
table.attr( 'id', function() { return this.id +"1"; })
alert($("#table1").children())
$("#table1").find(*).attr('id', function() { return this.id +"1"; })
table.appendTo("#table")
alert(document.getElementById("tr1"))
alert(document.getElementById("span1").value)
});
</script>
Run Code Online (Sandbox Code Playgroud) jquery ×1