<html>
<head>
<style>
table,th
{
border:1px solid black;
}
</style>
</head>
<body>
<table border="1">
<tr>
<th>单元格 1</th>
<th>单元格 2</th>
<th>单元格 3</th>
<th>单元格 4</th>
</tr>
</table>
<p>点击按钮返回表格行中每个单元格的位置。</p>
<p id="demo"></p>
<button onclick="myFunction()">尝试一下</button>
<script>
function myFunction()
{
var x = document.getElementsByTagName("th");
var txt = "";
for (var i=0; i<x.length; i++)
{
txt = txt + "单元格 "+(i+1)+" 的索引是: "+x[i].cellIndex+"<br>";
}
document.getElementById("demo").innerHTML=txt;
}
</script>
</body>
</html>