Javascript 将元素添加到dom中不会';行不通
当我尝试将元素添加到dom中时,它们不会被添加,但当我对它们进行console.log操作时,我会得到一个值Javascript 将元素添加到dom中不会';行不通,javascript,dom,Javascript,Dom,当我尝试将元素添加到dom中时,它们不会被添加,但当我对它们进行console.log操作时,我会得到一个值 for(c=0;c似乎要将th元素添加到当前空白的tfoot中?您可以尝试以下操作: for(c=0; c<2; c++) { document.querySelector('#achats_table tfoot').appendChild(document.createElement('th')); } for(c=0;c)可能是添加了它们,但由于它们内部没有任何内容
for(c=0;c似乎要将th元素添加到当前空白的tfoot中?您可以尝试以下操作:
for(c=0; c<2; c++) {
document.querySelector('#achats_table tfoot').appendChild(document.createElement('th'));
}
for(c=0;c)可能是添加了它们,但由于它们内部没有任何内容,所以您无法看到它们?您可以尝试通过将创建的元素存储在一个变量中来添加测试值,如下所示:var th=document.createElement('th');th.innerText='test';document.getElementById('achats_table')。tFoot.children[0]。appendChild(th)
@Kaddath我试过了,它不起作用,它们仍然没有添加。你需要显示html。如果tfoot
为空,那么它没有子项,并且该代码会抛出错误。你在控制台中查找错误了吗?
<table>
<thead>
<tr>
</tr>
</thead>
<tbody>
</tbody>
<tfoot>
<tr>
</tr>
</tfoot>
</table>
for(c=0; c<2; c++) {
document.querySelector('#achats_table tfoot').appendChild(document.createElement('th'));
}