td/th headers 屬性

定義和用法
headers 屬性可設置或者返回當前數據單元包含頭部資訊表頭的頭部列表。
這個屬性的值是引號包括的一列名稱,這些名稱是用 id 屬性定義的不同表頭單元格的名稱。
語法
設置 headers 屬性:
tdObject.headers="header_ids"
or
thObject.headers="header_ids"
or
thObject.headers="header_ids"
返回 headers 屬性:
tdObject.headers
or
thObject.headers
or
thObject.headers
提示: headers 屬性沒有默認值。
值 | 描述 |
---|---|
header_ids | 定義以空格為分割符的表頭單元id列表 |
流覽器支持
所有主要流覽器都支持 headers 屬性
實例
實例
顯示第二行表頭:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>IT研修(xuhuhu.com)</title>
<script>
function displayResult(){
var table=document.getElementById("myTable");
for (var i=0;i<table.rows[1].cells.length;i++){
alert(table.rows[1].cells[i].headers);
}
}
</script>
</head>
<body>
<table id="myTable" border="1" width="100%">
<tr>
<th id="name">名稱</th>
<th id="email">Email</th>
<th id="phone">電話</th>
<th id="addr">地址</th>
</tr>
<tr>
<td headers="name">John Doe</td>
<td headers="email">someone@example.com</td>
<td headers="phone">+45342323</td>
<td headers="addr">Rosevn 56,4300 Sandnes,Norway</td>
</tr>
</table>
<br>
<button type="button" onclick="displayResult()">獲取第二行的單元格表頭</button>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>IT研修(xuhuhu.com)</title>
<script>
function displayResult(){
var table=document.getElementById("myTable");
for (var i=0;i<table.rows[1].cells.length;i++){
alert(table.rows[1].cells[i].headers);
}
}
</script>
</head>
<body>
<table id="myTable" border="1" width="100%">
<tr>
<th id="name">名稱</th>
<th id="email">Email</th>
<th id="phone">電話</th>
<th id="addr">地址</th>
</tr>
<tr>
<td headers="name">John Doe</td>
<td headers="email">someone@example.com</td>
<td headers="phone">+45342323</td>
<td headers="addr">Rosevn 56,4300 Sandnes,Norway</td>
</tr>
</table>
<br>
<button type="button" onclick="displayResult()">獲取第二行的單元格表頭</button>
</body>
</html>
