AI编程助手
AI免费问答

如何使用 JavaScript 从 HTML 表中删除列?

WBOY   2023-08-23 23:57   948浏览 转载

如何使用 javascript 从 html 表中删除列?

在 HTML 中,表格用于以格式化的方式显示网页上的数据。该表包含显示数据的列和行。

有时,开发人员需要删除表列或允许用户删除它们。例如,该表包含用户数据并具有“年龄”和“生日”列。在这种情况下,我们需要删除“年龄”列,因为我们可以从“生日”列中获取年龄。

开发人员可以使用deleteCell()方法删除任何特定列。在本教程中,我们将学习通过索引、类名和列名删除表列。另外,开发人员应该记住表列是从零开始的索引。

语法

用户可以按照以下语法使用deleteCell()方法删除表格中的任何特定列。

for (var i = 0; i 
<p>在上面的语法中,我们迭代每个表格行并从“索引”中删除单元格。这里,‘index’是要删除的列的索引。</p>
<h3>示例1(按索引删除表格列)</h3>
<p>在下面的示例中,我们创建了包含单词数字的表格。此外,我们通过在 CSS 中提供边框来设计表格的样式。每当用户单击该按钮时,我们都会执行 deleteColumn() 函数。</p>
<p>在deleteColumn()函数中,我们使用id访问表。此外,我们使用“rows”属性获取表的所有行,并使用数组的“length”属性计算总行数。</p>
<p>我们用 2 初始化“index”变量来删除第三列。之后,我们使用 for 循环来遍历表的所有行。我们使用 deleteCell() 方法删除每行中的第三个单元格。</p>
<p>在输出中,用户可以单击按钮从表中删除第三列。</p>
<pre class="brush:html;toolbar:false;">

   <style>
      table {
          border-collapse: collapse;
        }
        td,
        th {
            border: 1px solid black;
            padding: 8px;
        }
   </style><h3> Using the <i> deleteCell() method </i> to delete the table columns using JavaScript </h3>
   
First Second Third Fourth
1 2 3 4
5 6 7 8
9 10 11 12


<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>

示例2(通过标题文本删除表格列)

在下面的示例中,我们创建了包含食物数据的表。在此示例中,我们使用标题文本删除表格列。

在deleteColum()函数中,我们访问表的标题。之后,我们访问所有标题行。接下来,我们迭代标题行并找到包含“卡路里”作为内部 HTML 的标题索引。

找到列索引后,我们可以使用 for 循环和 tableCell() 方法从每一行中删除特定单元格,就像我们在第一个示例中所做的那样。

在输出中,用户应单击按钮删除“卡路里”列。


   <style>
       table {border-collapse: collapse;}
       td, th {border: 1px solid black; padding: 8px; }
   </style><h3>Using the <i>deleteCell() method</i> to delete the table columns using JavaScript </h3>
   
Food Name Price Calories
Apple 99 95
Banana 89 105
Orange 79 85


<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>

示例3(通过类名删除多个表列)

在下面的示例中,我们将学习使用类名删除表格的多列。

在这里,我们首先访问表的所有行,然后使用 for 循环对其进行迭代。之后,我们访问包含“额外”类名的特定行的所有单元格。我们使用 while 循环遍历所有单元格,并使用“cells[0].parentNode.removeChild(cells[0])”一一删除所有单元格。

在上面的代码中,单元格[0]是当前单元格。 “parentNode”引用其行,removeChild() 方法从该行中删除子节点。

在输出中,用户可以单击按钮从表中删除多列。


    <style>
        table { border-collapse: collapse;}
        td, th {border: 1px solid black; padding: 8px;}
    </style><h3> Using the <i> deleteCell() method </i> to delete the table columns using JavaScript </h3>
    
Column 1 Column 2 Column 3 Column 4
Row 1, Column 1 Row 1, Column 2 Row 1, Column 3 Row 1, Column 4
Row 2, Column 1 Row 2, Column 2 Row 2, Column 3 Row 2, Column 4
Row 3, Column 1 Row 3, Column 2 Row 3, Column 3 Row 3, Column 4


<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>

我们学会了使用 JavaScript 从表中删除列。我们在前 2 个示例中通过将列索引作为参数传递来使用 deleteCeil() 方法。在第三个示例中,我们使用removeChild()方法来删除特定的单元格。

Java免费学习笔记:立即学习
解锁 Java 大师之旅:从入门到精通的终极指南

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除