首页 > 解决方案 > 向dom添加元素不起作用

问题描述

当我尝试将元素添加到我的 dom 时,它们没有被添加,但是当我 console.log 它们时,我确实得到了一个值<th class="text-right" rowspan="1" colspan="1"></th>

for(c=0; c<2; c++) {

console.log(document.getElementById('achats_table').tFoot.children[0].appendChild(document.createElement('th')));
    document.getElementById('achats_table').tFoot.children[0].appendChild(document.createElement('th'));
        }

表格 HTML:

<table>
  <thead>
    <tr>
    </tr>
  </thead>
  <tbody>
  </tbody>
  <tfoot>
    <tr>
    </tr>
  </tfoot>
</table>

我究竟做错了什么?我该如何调试呢?我没有控制台错误。

标签: javascriptdom

解决方案


看起来您想将 th 元素添加到当前空白的 tfoot 中?你可以试试这个:

for(c=0; c<2; c++) {
    document.querySelector('#achats_table tfoot').appendChild(document.createElement('th'));
}

推荐阅读