gpt4 book ai didi

javascript - 使用javascript向表中添加新行

转载 作者:行者123 更新时间:2023-11-29 19:04:18 25 4
gpt4 key购买 nike

我已经搜索并找到了类似的答案,但不完全是,我无法弄清楚我做错了什么......感谢您提供的任何帮助!

<html><head>
<script>
function doTheInsert() {
var newRow=document.getElementById('myTable').insertRow();
newRow="<td>New row text</td><td>New row 2nd cell</td>";
}
</script></head>
<body>
<table id="myTable" border="1">
<tr>
<td>First row</td>
<td>First row 2nd cell</td>
</tr>
<tr>
<td>Second row</td>
<td>more stuff</td>
</tr>
</table>
<br>
<input type="button" onclick="doTheInsert()" value="Insert new row">
</body></html>

最佳答案

我想为每个添加的单元格使用 insertCell 会更正式,但如果您设置 newRow 的 innerHTML,则只需放入整个字符串即可:

function doTheInsert() {
var newRow=document.getElementById('myTable').insertRow();
// newRow = "<td>New row text</td><td>New row 2nd cell</td>"; <-- won't work
newRow.innerHTML = "<td>New row text</td><td>New row 2nd cell</td>";
}

function doTheInsert() {
var newRow=document.getElementById('myTable').insertRow();
newRow.innerHTML="<td>New row text</td><td>New row 2nd cell</td>";
}
<table id="myTable" border="1">
<tr>
<td>First row</td>
<td>First row 2nd cell</td>
</tr>
<tr>
<td>Second row</td>
<td>more stuff</td>
</tr>
</table>

<input type="button" onclick="doTheInsert()" value="Insert new row">

关于javascript - 使用javascript向表中添加新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44141441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com