Javascript 单击按钮更改表格标题

Javascript 单击按钮更改表格标题,javascript,html,tableheader,Javascript,Html,Tableheader,我想写一个javascript函数,在点击按钮时更改html的名称。我的问题是,我不知道如何获取th的“文本” document.getElementById("id").text 及 不要工作。你应该使用innerHTML HTML: 请尝试document.getElementById(“id”).innerHTMLSee document.getElementById("id").value <table> <thead> <tr>

我想写一个javascript函数,在点击按钮时更改html的名称。我的问题是,我不知道如何获取
th
的“文本”

document.getElementById("id").text 


不要工作。

你应该使用
innerHTML

HTML:

请尝试document.getElementById(“id”).innerHTMLSee
document.getElementById("id").value 
<table>
  <thead>
    <tr>
      <th id="headId">Col name</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Info</td>
    </tr> 
  </tbody>
</table>

<input type="button" value="Click" onclick="getHeadName()" />
function getHeadName() {
    var headName = document.getElementById("headId").innerHTML
}