简体   繁体   English

向dom添加元素不起作用

[英]Adding element to dom doesn't work

When I try to add elements to my dom, they're not added, but when I console.log them, I do get a value <th class="text-right" rowspan="1" colspan="1"></th> 当我尝试将元素添加到我的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'));
        }

Table HTML : 表格HTML:

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

what am I doing wrong? 我究竟做错了什么? how can I debug this? 我该如何调试呢? I get no console errors. 我没有控制台错误。

It looks like you want to add th elements to a currently blank tfoot? 您似乎要向当前空白的脚添加元素? You could try this: 您可以尝试以下方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM