使用Javascript控制表格单元格的内部文本

时间:2017-11-08 07:20:27

标签: javascript html html-table

我有3张表如下:

<table id="timbercamptable">
    <tr>
        <th>Production</th>
        <th class="unitspertitle"></th>
        <th class="unitspernexttile"></th>
    </tr>   
    <tr>
        <td>Base production</td>
        <td class="currentunits"></td>
        <td class="nextunits"></td>
    </tr>
</table>

表的类和结构是相同的,但表id是不同的。我希望使用JavaScript控制类的innerText。调用这些类的正确方法是什么,因为知道另一个表的标识为sulphurpittablegoldminetable

例如,我想更改位于innerText

currentunits的{​​{1}}

1 个答案:

答案 0 :(得分:2)

您可以使用document.querySelector根据表格单元格的table idclass查询表格。

&#13;
&#13;
document.querySelector("#timbercamptable .currentunits").textContent = "100.00";
&#13;
<table id="timbercamptable">
    <tr>
        <th>Production</th>
        <th class="unitspertitle"></th>
        <th class="unitspernexttile"></th>
    </tr>
    <tr>
        <td>Base production</td>
        <td class="currentunits"></td>
        <td class="nextunits"></td>
    </tr>
</table>
&#13;
&#13;
&#13;