删除表中的单元格,html

Deleting cells in tables, html

本文关键字:html 单元格 删除      更新时间:2023-09-26

假设我在HTML中有一个5by5表,我希望能够用一个函数删除单元格,这样不仅单元格内的数据不可见,而且单元格占用的空间也不再可见:例如一个函数:(假设"CellId"是表单元格的Id,即表数据标记(

  function DeleteCell(CellId) {
document.getElementById("CellId").style.display = "none";
}

对我来说,这段代码只是删除单元格中的内容,但它所占用的空间并没有被删除。非常感谢您的帮助。

编辑:我不想用DeleteCell 去掉有问题的单元格

var removeTheCell = function(){
  var cell = document.getElementById("removeMe")
  cell.style.display = "none"
}
table, td {
  border: 1px black solid;
  }
<table>
  <tr>
    <th>
      Column 1
  </th>
  <th>
    Column 2
    </th>
  <th>
    Column 3
    </th>
    </tr>
<tr>
  <td>
    1
    </td>
  <td>
    Apples
    </td>
  <td>
    Red
    </td>
  </tr>
<tr>
  <td>
    2
    </td>
  <td id="removeMe">
    Pineapple
    </td>
  <td>
    Yellow
    </td>
  </table>
<button onclick="removeTheCell()">Remove Pineapple</button>

所以,也许我误解了你的问题,但在这个代码中,单元格占用的空间被删除了,这似乎与你发布的代码没有任何不同。让我知道我遗漏了什么,或者这是否有助于澄清问题。