Table createTHead() 方法
示例
建立 <thead> 元素(並向其中插入 <tr> 和 <td> 元素)
// 查詢 id="myTable" 的 <table> 元素
var table = document.getElementById("myTable");
// 建立一個空的 <thead> 元素並將其新增到表格中
var header = table.createTHead();
// 建立一個空的 <tr> 元素並將其新增到 <thead> 的第一個位置
var row = header.insertRow(0);
// 在“新”<tr> 元素的第一位置插入一個新單元格 (<td>)
var cell = row.insertCell(0);
// 在新單元格中新增一些粗體文字
cell.innerHTML = "<b>This is a table header</b>";
自己動手試一試 »
描述
createTHead() 方法建立一個空的 <thead> 元素並將其新增到表格中。
注意:如果表格中已存在 <thead> 元素,createTHead() 方法將返回現有的元素,而不會建立新的元素。
注意:<thead> 元素內部必須包含一個或多個 <tr> 標籤。
提示:要從表格中刪除 <thead> 元素,請使用 deleteTHead() 方法。
瀏覽器支援
方法 | |||||
---|---|---|---|---|---|
createTHead() | 是 | 是 | 是 | 是 | 是 |
語法
tableObject.createTHead()
引數
無 |
技術詳情
返回值 | 新建立的(或已存在的)<thead> 元素 |
---|
更多示例
示例
建立和刪除 <thead> 元素
function myCreateFunction() {
var table = document.getElementById("myTable");
var header = table.createTHead();
var row = header.insertRow(0);
var cell = row.insertCell(0);
cell.innerHTML = "<b>This is a table header</b>";
}
function myDeleteFunction() {
document.getElementById("myTable").deleteTHead();
}
自己動手試一試 »
相關頁面
HTML 參考:HTML <thead> 標籤
↜ Table 物件