HTML表格单元格可编辑及删除内容实现方法
要设置表格中的单元格可编辑,可以使用HTML的contenteditable属性。将该属性设置为'true',即可使单元格可编辑。
示例代码如下:
<table>
<tr>
<td contenteditable='true'>可编辑</td>
<td contenteditable='true'>可编辑</td>
<td contenteditable='true'>可编辑</td>
</tr>
<tr>
<td contenteditable='true'>可编辑</td>
<td contenteditable='true'>可编辑</td>
<td contenteditable='true'>可编辑</td>
</tr>
</table>
要实现删除单元格内容的功能,可以结合JavaScript来实现。通过给单元格绑定事件,当用户点击删除按钮时,清空单元格的内容。
示例代码如下:
<table>
<tr>
<td contenteditable='true' id='cell1'>可编辑</td>
<td contenteditable='true' id='cell2'>可编辑</td>
<td contenteditable='true' id='cell3'>可编辑</td>
</tr>
<tr>
<td contenteditable='true' id='cell4'>可编辑</td>
<td contenteditable='true' id='cell5'>可编辑</td>
<td contenteditable='true' id='cell6'>可编辑</td>
</tr>
</table>
<button onclick='deleteContent('cell1')'>删除内容</button>
<script>
function deleteContent(cellId) {
document.getElementById(cellId).textContent = '';
}
</script>
在上面的示例中,我们给单元格添加了id属性,通过JavaScript的getElementById方法获取到单元格元素,然后将其textContent属性设置为空字符串,即可清空单元格内容。
原文地址: http://www.cveoy.top/t/topic/AmP 著作权归作者所有。请勿转载和采集!