小码哥的IT人生

HTML DOM THead 对象

JavaScript基础 2022-05-13 16:23:30小码哥的IT人生shichen

HTML DOM THead 对象

THead 对象

THead 对象代表 HTML <thead> 元素。

访问 THead 对象

您可使用 getElementById() 来访问 <thead> 元素:

示例代码:

var x = document.getElementById("myTHead");

完整实例:

<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
  border: 1px solid black;
}
</style>
</head>
<body>
<h3>如何访问 THEAD 元素的演示</h3>
<table>
 <thead id="myTHead">
   <tr>
     <th>Header 1</th>
     <th>Header 2</th>
   </tr>
  </thead>
 <tbody>
   <tr>
     <td>Cell 1</td>
     <td>Cell 2</td>
   </tr>
   <tr>
     <td>Cell 3</td>
     <td>Cell 4</td>
   </tr>
  </tbody>
</table>
<p>单击该按钮可更改 THEAD 元素的样式。</p>
<button onclick="myFunction()">试一试</button>
<p id="demo"></p>
<script>
function myFunction() {
  var x = document.getElementById("myTHead");
  x.style.color = "red";
}
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

创建 THead 对象

您可使用 document.createElement() 方法来创建 <thead> 元素:

示例代码:

var x = document.createElement("THEAD");

完整实例:

<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
  border: 1px solid black;
}
</style>
</head>
<body>
<table id="myTable">
 <tbody>
   <tr>
     <td>Cell 1</td>
   </tr>
   <tr>
     <td>Cell 2</td>
   </tr>
  </tbody>
</table>
<p>单击该按钮创建一个 THEAD 元素(以及一个 TR 和一个 TD)。</p>
<button onclick="myFunction()">试一试</button>
<script>
function myFunction() {
  var x = document.createElement("THEAD");
  var y = document.createElement("TR");
  var z = document.createElement("TD");
  z.innerHTML = "Header"
  y.appendChild(z);
  x.appendChild(y);
  document.getElementById("myTable").appendChild(x);
}
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

THead 对象属性

属性 描述
align

HTML5 中不支持。请改用 style.textAlign。

设置或返回 thead 元素中内容的水平对齐方式。

ch

HTML5 中不支持。

设置或返回 thead 元素内的对齐字符。

chOff

HTML5 中不支持。

设置或返回 ch 属性的水平偏移量。

vAlign

HTML5 中不支持。请改用 style.verticalAlign。

设置或返回 thead 元素中内容的垂直对齐方式。

标准属性和事件

THead 对象支持标准属性事件

相关页面

HTML 教程:HTML 表格

HTML 参考手册:HTML <thead> 标签

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024