HTML DOM TableHeader 对象

htmldomweb developmentfront end technology更新于 2025/11/29 11:24:17

HTML DOM TableHeader 对象表示 HTML 文档的第 <th> 个元素。

创建 TableHeader 对象

语法

以下是语法 −

document.createElement(“TH”);

TableHeader 对象的属性

属性说明
cellIndex返回表格行单元格集合中单元格的位置。
colSpan返回并修改表格 colspan 属性的值。
headers返回并修改表格 headers 属性的值。
abbr返回并修改表格 abbr 属性的值。
rowSpan返回并修改表格 rowspan 属性的值表格

让我们看一个 TableHeader 对象的示例:

示例

<!DOCTYPE html>
<html>
<style>
   body {
      color: #000;
      background: lightblue;
      height: 100vh;
      text-align: center;
   }
   table {
      margin: 2rem auto;
      width: 400px;
   }
   .btn {
      background: #db133a;
      border: none;
      height: 2rem;
      border-radius: 2px;
      width: 40%;
      display: block;
      color: #fff;
      outline: none;
      cursor: pointer;
      margin: 1rem auto;
   }
</style>
<body>
<h1>DOM TableHeader Object Demo</h1>
<table border="2">
<thead>
<tr class="table-header">
</tr>
<thead>
<tbody>
<tr>
<td>John</td>
<td>English</td>
</tr>
<tr>
<td>Elon</td>
<td>Germany</td>
</tr>
</tbody>
</table>
<button onclick="get()" class="btn">Create a TableHeader</button>
<script>
   function get() {
      var th1 = document.createElement("TH");
      var th2 = document.createElement("TH");
      th1.innerHTML = "Name";
      th2.innerHTML = "Language";
      document.querySelector('.table-header').appendChild(th1);
      document.querySelector('.table-header').appendChild(th2);
   }
</script>
</body>
</html>

输出

点击"创建 TableHeader"按钮来创建 TableHeader 对象。


相关文章


有用资源