javascript - 无法使用innerHTML通过JavaScript更新HTML表格

标签 javascript html

我是 JavaScript 初学者,我想使用 .innerHTML 从表格中打印一些文本,但它不起作用。代码如下:

<table style="width:100%">
    <tr>
        <td id="tr">&nbsp;</td>
        <td id="tr1">&nbsp;</td>
        <td id="tr2">&nbsp;</td>
    </tr>
    <tr>
       <td id="tr3">&nbsp;</td>
       <td id="tr4">&nbsp;</td>
       <td id="tr5">&nbsp;</td>
    </tr>
    <tr>
       <td id="tr6">&nbsp;</td>
       <td id="tr7">&nbsp;</td>
       <td id="tr8">&nbsp;</td>
    </tr>
</table>

<script>
document.getElementById("tr").innerHTML = "some text";
document.getElementById("tr2").innerHTML = "some text1";
document.getElementById("tr2").innerHTML = "some text2";
document.getElementById("tr3").innerHTML = "some text3";
document.getElementById("tr4").innerHTML = "some text4";
document.getElementById("tr5").innerHTML = "some text5";
document.getElementById("tr7").innerHTML = "some text6";
document.getElementById("tr8").innerHTML = "some text7";
document.getElementById("tr9").innerHTML = "some text8";
</script>

最佳答案

您错过了 tr6,并且 tr9 不存在。 Here is a link to a working snapshot of your code .
不过,我建议您了解循环的工作原理。

关于javascript - 无法使用innerHTML通过JavaScript更新HTML表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36106870/

相关文章:

javascript - WordPress:Ajax 发布请求没有获得值(value)

javascript - 在另一个元素之后添加 HTML 元素

javascript - iFrameResizer 不计算 iframe 的高度

javascript - Angular 模型未在动画完整回调上更新

javascript - 如何在Ajax中正确调用这个函数?

javascript - Angularjs - 与标准 html 元素同名的自定义指令

html - 水平 float 元素在 chrome 中被向下推,但在 firefox 中没有

javascript - 从循环内的父上下文更改/更新 Handlebars JS 中的变量

javascript - 如何在 javascript 中获取 html 标签值 (div)

javascript - javascript中element.focus()方法的结果是什么