我需要一些帮助来编写一些jQuery代码。
我需要在点击时动态克隆一个表格。但是每次都需要更改表格及其子元素的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>