javascript - 向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>
我究竟做错了什么?我该如何调试呢?我没有控制台错误。
解决方案
看起来您想将 th 元素添加到当前空白的 tfoot 中?你可以试试这个:
for(c=0; c<2; c++) {
document.querySelector('#achats_table tfoot').appendChild(document.createElement('th'));
}
推荐阅读
- python - 在python中有多个变量的条件语句
- scala - 如何在运行时使用 ToolBox.eval 实例化用户定义的类
- php - 仅在满足 If 条件时将键和值添加到数组
- azure-devops-rest-api - 如何使用 REST API 和 Microsoft Flow 将文件从 Outlook 上传到 Azure DevOps WorkItem?
- jenkins - 使用 API 在多分支管道内创建新作业
- json - 如何获取特定 JSON 值的架构?
- react-native - React-Native 导航错误:我正在尝试运行反应导航示例并遇到此错误
- c# - Unity Container 注册并解析具有继承的通用接口
- javascript - 为什么正数右移有时会导致负数?
- bash - 如何检查 docker 容器中是否设置了环境变量?