【发布时间】:2021-08-06 09:44:53
【问题描述】:
我编写了在单击按钮时在表中添加新行的代码。如果我输入值然后单击按钮,则数据将被删除。为什么?
document.getElementById('add').addEventListener('click', function(){
document.querySelector('table tbody').innerHTML += document.querySelector('table tbody tr').innerHTML;
});
<button id="add">Add</button>
<table>
<thead>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="number" placeholder="test" min="1"></td>
<td><input type="number" placeholder="test" min="1"></td>
<td><input type="number" placeholder="test" min="1"></td>
<td><input type="number" placeholder="test" min="1"></td>
<td><input type="checkbox" checked></td>
</tr>
</tbody>
</table>
【问题讨论】:
标签: javascript html html-table