删除 HTML 表格中的所有行 [英] Delete all rows in an HTML table

查看:43
本文介绍了删除 HTML 表格中的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Javascript 删除 HTML 表中除 之外的所有行,并且不循环遍历表中的所有行?我有一个非常大的表,我不想在遍历行以删除它们时冻结 UI

How can I delete all rows of an HTML table except the <th>'s using Javascript, and without looping through all the rows in the table? I have a very huge table and I don't want to freeze the UI while I'm looping through the rows to delete them

推荐答案

行保留在 中,其他行保留在 然后用一个新的空的替换 .

Keep the <th> row in a <thead> and the other rows in a <tbody> then replace the <tbody> with a new, empty one.

var new_tbody = document.createElement('tbody');
populate_with_new_rows(new_tbody);
old_tbody.parentNode.replaceChild(new_tbody, old_tbody)

这篇关于删除 HTML 表格中的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆