Table createTHead() 方法

Table 對象參考手冊 Table 對象

定義和用法

createTHead() 方法用於在表格中獲取或創建 <thead> 元素。

注意: 如果 thead 元素如果在表格中已經存在, createTHead() 方法返回存在的值,不返回新的<thead> 元素

提示:在表格中移除 thead 元素請使用 deleteTHead() 方法。

語法

tableObject.createTHead()


流覽器支持

Internet ExplorerFirefoxOperaGoogle ChromeSafari

所有主要流覽器都支持 createTHead() 方法


實例

實例

創建和刪除 thead 元素:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>IT研修(xuhuhu.com)</title>
<script>
function createTHead(){
    var x=document.getElementById("myTable");
    if (!x.tHead){
    var header=x.createTHead();
    var row=header.insertRow(0);
    var cell=row.insertCell(0);
    cell.innerHTML="<b>這是個表頭</b>";
    }
}
function deleteTHead(){
    document.getElementById("myTable").deleteTHead();
}
</script>
</head>
<body>

<table id="myTable" border="1">
    <tr>
        <td>cell 1</td>
        <td>cell 2</td>
    </tr>
    <tr>
        <td>cell 3</td>
        <td>cell 4</td>
    </tr>
</table>
<br>
<button type="button" onclick="createTHead()">創建表頭</button>
<button type="button" onclick="deleteTHead()">刪除表頭</button>

</body>
</html>



Table 對象參考手冊 Table 對象