Table createTHead()Method

Table 对象参考手册Table Object

Definition and Usage

The createTHead() method is used to get or create a <thead> element in a table.

Note:If the thead element already exists in the table, the createTHead() method returns the existing value and does not return a new <thead> element.

Tip:To remove a thead element from a table, use thedeleteTHead()method.

Syntax

tableObject.createTHead()


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the createTHead() method.


Example

Example

Create and delete thead element:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Rookie Tutorial (example.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>This is a table header</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()">Create Table Header</button>
<button type="button" onclick="deleteTHead()">Delete Table Header</button>

</body>
</html>

Try it Yourself »


Table 对象参考手册Table Object Other Extensions