
点击表头删除表头所属列
问题描述:
如何实现点击表格表头后删除表头所属的那一列?
解决方案:
方法一:使用原生 javascript
示例代码:
<table>
<thead>
<tr>
<th onclick="deletecolumn(this)">列 1</th>
<th onclick="deletecolumn(this)">列 2</th>
<th onclick="deletecolumn(this)">列 3</th>
</tr>
</thead>
<tbody>
<!-- 表格数据 -->
</tbody>
</table>
<script>
function deletecolumn(th) {
// 获取列索引
const index = th.cellindex;
// 删除列
th.parentelement.deletecell(index);
}
</script>方法二:使用 jquery
示例代码:
<table>
<thead>
<tr>
<th class="delete-column">列 1</th>
<th class="delete-column">列 2</th>
<th class="delete-column">列 3</th>
</tr>
</thead>
<tbody>
<!-- 表格数据 -->
</tbody>
</table>
<script>
$(function() {
$('.delete-column').click(function() {
// 获取列索引
const index = $(this).index();
// 删除列
$(this).parent().siblings('td:nth-child(' + (index + 1) + ')').remove();
$(this).remove();
});
});
</script>以上就是如何通过点击表格表头删除整列数据?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号