我有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
。调用这些类的正确方法是什么,因为知道另一个表的标识为sulphurpittable
和goldminetable
。
例如,我想更改位于innerText
currentunits
的{{1}}
答案 0 :(得分:2)
您可以使用document.querySelector
根据表格单元格的table id
和class
查询表格。
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;