<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>tr中分别获得每个td的元素</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
<script type="text/javascript">
</script>
</head>
<body>
</html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>tr中分别获得每个td的元素</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
<script type="text/javascript">
window.onload = function() {
//第一种方法
var row = document.getElementById("row1");
// 找到这个tr下的所有td,不能用childNodes 属性,部分浏览器不兼容
var cells = row.getElementsByTagName("td");
for(var i = 0; i < cells.length; i++) {
alert("第"+(i+1)+"格的数字是"+cells[i].innerHTML);
}
//第二种方法,操作表格比较标准的代码
var tab = document.getElementById("table1");
var rows = tab.rows; //取得这个table下的所有行
for (var i = 0; i < rows.length; i++) {
for (var j = 0; j < rows[i].cells.length; j++) {
var cell = rows[i].cells[j];alert("第"+(i+1)+"行第"+(j+1)+"格的数字是"+cell.innerHTML);
}
}}
</script>
</head>
<body>
<table border="1" id="table1">
<tr id="row1">
<td>01</td><td>02</td>
</tr><tr id="row2">
<td>11</td><td>12</td>
</tr>
</table></body>
</html>