<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
}
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<p>单击该按钮可返回第二行中每个 td 元素的 headers 属性的值。</p>
<table id="myTable">
<tr>
<th id="name">姓名</th>
<th id="email">电邮</th>
<th id="phone">电话</th>
<th id="address">地址</th>
</tr>
<tr>
<td headers="name">Bill Gates</td>
<td headers="email">someone@example.com</td>
<td headers="phone">(800) 426-9400</td>
<td headers="address">1 Microsoft Way Redmond, WA 98052 USA</td>
</tr>
</table>
<br>
<button onclick="myFunction()">试一试</button>
<p id="demo"></p>
<script>
function myFunction() {
var table = document.getElementById("myTable");
var txt = "";
var i;
for (i = 0; i < table.rows[1].cells.length; i++) {
txt = txt + table.rows[1].cells[i].headers + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>