如何使用 JavaScript 删除 HTML 表格中的列?
在 HTML 中,表格用于以格式化的方式在网页上显示数据。表格包含列和行来显示数据。
有时,开发者需要删除表格列或允许用户删除它们。例如,表格包含用户数据并具有“年龄”和“出生日期”列。在这种情况下,我们需要删除“年龄”列,因为我们可以从“出生日期”列获取年龄。
开发者可以使用 deleteCell() 方法删除任何特定列。在本教程中,我们将学习按索引、类名和列名删除表格列。此外,开发者应记住,表格列是基于零的索引。
语法
用户可以按照以下语法使用 deleteCell() 方法删除表格的任何特定列。
for (var i = 0; i < rowCount; i++) {
table.rows[i].deleteCell(index);
}
在上面的语法中,我们迭代每一行表格并从“索引”中删除单元格。“索引”是要删除的列的索引。
示例 1(按索引删除表格列)
在下面的示例中,我们创建了一个包含数字单词的表格。我们还通过在 CSS 中添加边框来设置表格样式。每当用户单击按钮时,我们都会执行 deleteColumn() 函数。
在 deleteColumn() 函数中,我们使用 id 访问表格。我们还使用“rows”属性获取表格的所有行,并使用数组的“length”属性计算行的总数。
我们将“index”变量初始化为 2 以删除第三列。之后,我们使用 for 循环迭代表格的所有行。我们使用 deleteCell() 方法从每一行删除第三个单元格。
在输出中,用户可以单击按钮删除表格中的第三列。
<html>
<head>
<style>
table {
border-collapse: collapse;
}
td,
th {
border: 1px solid black;
padding: 8px;
}
</style>
</head>
<body>
<h3> Using the <i> deleteCell() method </i> to delete the table columns using JavaScript </h3>
<table id = "test">
<tr>
<th> First </th>
<th> Second </th>
<th> Third </th>
<th> Fourth </th>
</tr>
<tr>
<td> 1 </td>
<td> 2 </td>
<td> 3 </td>
<td> 4 </td>
</tr>
<tr>
<td> 5 </td>
<td> 6 </td>
<td> 7 </td>
<td> 8 </td>
</tr>
<tr>
<td> 9 </td>
<td> 10 </td>
<td> 11 </td>
<td> 12 </td>
</tr>
</table>
<br> <br>
<button onclick = "deleteColumn()"> Delete Column </button>
<script>
function deleteColumn() {
var table = document.getElementById("test");
var rowCount = table.rows.length;
let index = 2;
for (var i = 0; i < rowCount; i++) {
table.rows[i].deleteCell(index);
}
}
</script>
</html>
示例 2(按标题文本删除表格列)
在下面的示例中,我们创建了一个包含食品数据的表格。在这个示例中,我们使用标题文本删除表格列。
在 deleteColum() 函数中,我们访问表格的表头。之后,我们访问所有表头行。接下来,我们遍历表头行并查找包含“卡路里”作为内部 HTML 的表头的索引。
找到列索引后,我们可以使用 for 循环和 tableCell() 方法删除每一行中的特定单元格,就像我们在第一个示例中所做的那样。
在输出中,用户应单击按钮删除“卡路里”列。
<html>
<head>
<style>
table {border-collapse: collapse;}
td, th {border: 1px solid black; padding: 8px; }
</style>
</head>
<body>
<h3>Using the <i>deleteCell() method</i> to delete the table columns using JavaScript </h3>
<table id="food">
<tr>
<th>Food Name</th>
<th>Price</th>
<th>Calories</th>
</tr>
<tr>
<td>Apple</td>
<td>99</td>
<td>95</td>
</tr>
<tr>
<td>Banana</td>
<td>89</td>
<td>105</td>
</tr>
<tr>
<td>Orange</td>
<td>79</td>
<td>85</td>
</tr>
</table>
<br> <br>
<script>
function deleteColumn() {
// get a table
var table = document.getElementById('food');
// get header row
var headerRow = table.getElementsByTagName('tr')[0];
// get headers
var headers = headerRow.getElementsByTagName('th');
// find column index
for (var i = 0; i < headers.length; i++) {
if (headers[i].innerHTML === "Calories") {
var columnIndex = i;
break;
}
}
// use column index to delete cells
if (columnIndex !== undefined) {
var rowCount = table.rows.length;
for (var i = 0; i < rowCount; i++) {
table.rows[i].deleteCell(columnIndex);
}
}
}
</script>
<button onclick="deleteColumn()">Delete Column</button>
</body>
</html>
示例 3(按类名删除多个表格列)
在下面的示例中,我们将学习如何使用类名删除表格的多个列。
在这里,我们首先访问表格的所有行,并使用 for 循环遍历它们。之后,我们访问包含“extra”类名的特定行的所有单元格。我们使用 while 循环遍历所有单元格,并使用“cells[0].parentNode.removeChild(cells[0])”逐个删除所有单元格。
在上面的代码中,cell[0] 是当前单元格。“parentNode”指的是它的行,“removeChild()”方法从该行中删除子节点。
在输出中,用户可以单击按钮删除表格中的多个列。
<html>
<head>
<style>
table { border-collapse: collapse;}
td, th {border: 1px solid black; padding: 8px;}
</style>
</head>
<body>
<h3> Using the <i> deleteCell() method </i> to delete the table columns using JavaScript </h3>
<table id = "HTMLtable">
<tr>
<th> Column 1 </th>
<th class = "extra"> Column 2 </th>
<th >Column 3 </th>
<th class = "extra"> Column 4 </th>
</tr>
<tr>
<td> Row 1, Column 1 </td>
<td class = "extra"> Row 1, Column 2 </td>
<td> Row 1, Column 3 </td>
<td class = "extra"> Row 1, Column 4 </td>
</tr>
<tr>
<td> Row 2, Column 1 </td>
<td class = "extra"> Row 2, Column 2 </td>
<td> Row 2, Column 3 </td>
<td class = "extra"> Row 2, Column 4 </td>
</tr>
<tr>
<td> Row 3, Column 1 </td>
<td class = "extra"> Row 3, Column 2 </td>
<td> Row 3, Column 3 </td>
<td class = "extra"> Row 3, Column 4 </td>
</table> <br> <br>
<button onclick="deleteColumn()"> Delete Column </button>
<script>
function deleteColumn() {
var table = document.getElementById('HTMLtable');
var rows = table.getElementsByTagName('tr');
// iterate through rows
for (var i = 0; i < rows.length; i++) {
// get cells with className 'extra'
var cells = rows[i].getElementsByClassName('extra');
// delete cells
while (cells.length > 0) {
cells[0].parentNode.removeChild(cells[0]);
}
}
}
</script>
</html>
我们学习了如何使用 JavaScript 删除表格中的列。我们在前两个示例中使用 deleteCeil() 方法并将列索引作为参数传递。在第三个示例中,我们使用 removeChild() 方法删除特定单元格。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP