如何在JavaScript中更改子节点?

如何在JavaScript中更改子节点?,javascript,clone,Javascript,Clone,单击按钮时,如何克隆表格并自动增加#更改此编号 这是我的问题的例子 <button type="button" onclick="cloneTable()">clone Table</button> <table> <tr> <th id="changeThis">1</th> <td><select name="" size="1"><option></opti

单击按钮时,如何克隆表格并自动增加
#更改此
编号

这是我的问题的例子

<button type="button" onclick="cloneTable()">clone Table</button>
<table>
  <tr>
    <th id="changeThis">1</th>
    <td><select name="" size="1"><option></option></select><input type="text"></td>
  </tr>
</table>
克隆表
1.

谢谢

您必须使用
cloneNode
方法

var i=1;
函数cloneTable(){
var table=document.getElementById(“表1”);
var clone=table.cloneNode(true);
clone.id=“table”+++i;
document.body.appendChild(克隆);
document.querySelector(“#table”+i+“th”).innerHTML=i;
}
克隆表

1
你尝试过什么吗?请阅读StackOverflow中的查询我不认为so@Manish只需复制粘贴他的硬件:(我想你搞错了,他想更改th的
#change this
id,而不是表。因为根据你的逻辑,仍然会有多个th具有相同的id,因此违反了标准。@Manish,现在看看我的答案。@Fun,别忘了接受答案以帮助其他人。