<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<script language="javascript">
function test()
{
var table1=document.createElement("table");
table1.setAttribute("id","DataTable");
table1.width="20%";
table1.border="1";
table1.cellPadding=0;
table1.cellSpacing=0;
for(var i=0;i<10;i++)
{
var tr1=document.createElement("tr");
var td1=document.createElement("td");
td1.setAttribute("id","td"+i); //?
td1.appendChild(document.createTextNode("TableRows"));
tr1.appendChild(td1);
table1.appendChild(tr1);
}
document.getElementById("body1").appendChild(table1);
alert("RowNumber="+table1.rows.length); //0
//如何取得table1的行数以及行的id?
} </script>
<body id="body1" onload="test()">
</body>
</html>
rows.length;//行数
rows[0]//第一行
rows[0].cells[0]//第一行第一列
alert('一共'+trs.length+'行')
for(var i = 0; i < trs.length;i++){
alert(trs[i].id)
}
var rows = table1.rows ;
for(var i=0;i<rows.length;i++){
var cells = rows[i].cells ;
for(var k=0;k<cells.length;k++){
if(cells[k].id!=""){
alert("第" + rows[i].rowIndex + "行,ID=" + cells[k].id); //0
}
}
}