javascript - 如何使用 JavaScript 将 <td> 标签添加到我的 html 表格中?

标签 javascript html

我正在尝试使用 insertcell 方法将一列添加到我的表中,但要么我的语法有误,要么它不起作用。我想知道是否有人可以解释我哪里出错了?

html 中的表体由一些其他 JavaScript 动态填充,但我认为这不是问题,因为我已经测试过使用警告框从该表中抓取一些内容并且它有效(在下面注释掉):

<!DOCTYPE html>
<script type="text/javascript" src="fullstationxyparser.js">
</script>
<html>
<body>
    <table border=1>
        <thead>
            <tr>
                <td>Element Name</td>
                <td>x</td>
                <td>y</td>
                <td>testCol</td>
            </tr>
        </thead>
        <tbody id="stationlist">
        </tbody>
    </table>
</body>
</html>
function addStationNames() {
    var myTable = document.getElementById("stationlist");
    var stationListRows = myTable.getElementsByTagName('tr');

    for (var i = 1; i < stationListRows.length; i++) {
        var cell = stationListRows[i].getElementsByTagName('td');
        var stationName = cell[0].innerHTML; //get station id from element Name column
        var currentRow = stationListRows[i];
        var newCol = currentRow.insertcell(-1);
        newCol.innerHTML = stationName;
        //alert(stationName);
    }
}

在 Firefox 开发人员工具中,我收到 TypeError: "currentRow.insertcell is not a function"。也许我不能在行集合上使用 insertcell 方法?

最佳答案

一般您可以调用 insertRow() Table DOM 元素上的方法,然后调用 insertCell() 方法如下图动态添加<td>使用 JavaScript 标记到您的表格。

小心调用insertCell() (大写字母 C)而不是 insertcell()正如你目前所做的那样:

const table = document.querySelector('table');

/* Insert new row */
const row = table.insertRow();

/* Insert cells (td) for row */
const td0 = row.insertCell(0);
const td1 = row.insertCell(1);
const td2 = row.insertCell(2);
const td3 = row.insertCell(3);

/* Populate cells with data */
td0.innerText = 'Foo';
td1.innerText = '3';
td2.innerText = '6';
td3.innerText = 'success';
<table border="1">
  <thead>
    <tr>
      <td>Element Name</td>
      <td>x</td>
      <td>y</td>
      <td>testCol</td>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

特定于您的代码,需要考虑的其他一些更改可能已列在以下代码片段中:

function addStationNames() {

  /* Condense table row access into single query */
  const stationRows = document.querySelectorAll("#stationlist tr");
  
  stationRows.forEach((stationRow, i) => {
    
    /* Skip first row */
    if(i === 0) { return; }
    
    /* Get station name from text of first cell */
    const stationName = stationRow.querySelector('td:first-child').innerText;
    
    /* Insert last cell on row and assign station name */
    stationRow.insertCell(-1).innerText = stationName;
  });
  
  /*
  Old code:
  for (let i = 1; i < stationListRows.length; i++) {    
    var cell = stationListRows[i].getElementsByTagName('td');
    var stationName = cell[0].innerHTML; 
    var currentRow = stationListRows[i];
    var newCol = currentRow.insertcell(-1);
    newCol.innerHTML = stationName;
  }
  */
}

addStationNames();
<!-- set table id to stationlist -->
<table border="1" id="stationlist">
  <thead>
    <tr>
      <td>Element Name</td>
      <td>x</td>
      <td>y</td>
      <td>testCol</td>
    </tr>
    <tr>
      <td>90's pop</td>
      <td>232</td>
      <td>543</td> 
    </tr>
  </thead>
  <tbody>
    <!-- Remove id from tbody -->
  </tbody>
</table>

关于javascript - 如何使用 JavaScript 将 <td> 标签添加到我的 html 表格中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56533925/

相关文章:

javascript - JSON 字符串中出现意外的空键值

javascript - 如何通过<th>值获取表<td>内容

Javascript 404 错误检测

html - 如何将表单中的字段放置为具有相同的高度和宽度

php - 更新功能不起作用

html - 使用 CSS 的段落中每个句点后有两个空格?

javascript - 溢出 :hidden, 我在这里做错了什么

javascript - 如何将图像/图标添加到 Highcharts 中的图表

html - 当我调整浏览器窗口大小时,它会覆盖我的幻灯片内容 - Bootstrap

html - float 权利问题