急送分·用一个function读出表格里每行每列的字符,该怎么写?
用一个function读出下面表格里每行每列的字符,该怎么写?
<table width= "200 " border= "0 " align= "center ">
<tr>
<td align= "center "> 1 </td>
<td align= "center "> 2 </td>
<td align= "center "> 3 </td>
</tr>
<tr>
<td align= "center "> 4 </td>
<td align= "center "> 5 </td>
<td align= "center "> 6 </td>
</tr>
<tr>
<td align= "center "> 7 </td>
<td align= "center "> 8 </td>
<td align= "center "> 9 </td>
</tr>
</table>
------
<script type= "text/javascript ">
function table()
{
???...
}
</script>
------解决方案-------------------- <script type= "text/javascript ">
function table()
{
var dom = document.getElementById( "table1 ");
var domTr = dom.getElementsByTagName( "tr ");
for(var i = 0; i < domTr.length; i++)
{
var domTd = domTr[i].getElementsByTagName( "td ");
for(var j =0 ;j < domTd.length;j++)
{
alert(domTd[j].innerHTML);
}
}
}
table();
</script>
------解决方案--------------------//整个取出来
function method1(){
var table = document.getElementById( 'tab1 ');
for(var i=0;i <table.cells.length;i++)
alert(table.cells[i].firstChild.nodeValue)
}
//一行一行的取
function method2(){
var table = document.getElementById( 'tab1 ');
for(var i=0;i <table.rows.length;i++){
alert( '第 '+(i+1)+ '行 ');
for(var j=0;j <table.rows[i].cells.length;j++)
alert(table.rows[i].cells[j].firstChild.nodeValue);
}
}
要给table加个id= 'tab1 '