Table createTHead() 方法
定義和用法
createTHead()
方法創建空的 <thead> 元素 并將其添加到表中。
注釋:如果表中已經存在 <thead> 元素,則 createTHead()
方法返回現有元素,而不創建新元素。
注釋:<thead> 元素內部必須有一個或多個 <tr> 標簽。
提示:如需從表中刪除 <thead> 元素,請使用 deleteTHead() 方法。
另請參閱:
HTML 參考手冊:HTML <thead> 標簽
實例
例子 1
創建 <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>";
例子 2
創建和刪除 <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(); }
語法
tableObject.createTHead()
參數
無。
技術細節
返回值: | 新創建的(或現有的)<thead> 元素。 |
---|
瀏覽器支持
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
支持 | 支持 | 支持 | 支持 | 支持 |