如何更改表格中某列的内容?

时间:2017-08-05 17:07:32

标签: javascript

我想更改"开始日期"的内部文本这张桌子上的专栏。

以下代码:

<tr id="currentRow">
  <th>Name</th>
  <th>Position</th>
  <th>Office</th>
  <th>Age</th>
  <th>Start date</th>
  <th>Salary</th>           
</tr> 

我在下面尝试了这段代码,但它没有用。

document.getElementById("currentRow").td[4] = "this is a text";

我怎样才能实现它?

2 个答案:

答案 0 :(得分:0)

如果您想使用.innerHTML更改所选元素的内容,则需要添加querySelectorAll,例如:

document.querySelectorAll('#currentRow th')[4].innerHTML = "this is a text";

注意:确保您的表格至少包含一个td

希望这有帮助。

document.querySelectorAll('#currentRow th')[4].innerHTML = "this is a text";
<table>
  <tr id="currentRow">
    <th>Name</th>
    <th>Position</th>
    <th>Office</th>
    <th>Age</th>
    <th>Start date</th>
    <th>Salary</th>

  </tr> 
</table>

答案 1 :(得分:0)

&#13;
&#13;
unexpected token: Binary
&#13;
&#13;
&#13;

元素中没有 td ,而是 children 数组。