这就是我在javascript中阅读表格的过程。基本上我钻入行中,然后我可以深入每行的单个单元格。 这应该给你一个想法
//gets table
var oTable = document.getElementById('myTable');
//gets rows of table
var rowLength = oTable.rows.length;
//loops through rows
for (i = 0; i < rowLength; i++){
//gets cells of current row
var oCells = oTable.rows.item(i).cells;
//gets amount of cells of current row
var cellLength = oCells.length;
//loops through each cell in current row
for(var j = 0; j < cellLength; j++){
/* get your cell info here */
/* var cellVal = oCells.item(j).innerHTML; */
}
}
修订版 - 测试脚本
<table id="myTable">
<tr>
<td>A1</td>
<td>A2</td>
<td>A3</td>
</tr>
<tr>
<td>B1</td>
<td>B2</td>
<td>B3</td>
</tr>
</table>
<script>
//gets table
var oTable = document.getElementById('myTable');
//gets rows of table
var rowLength = oTable.rows.length;
//loops through rows
for (i = 0; i < rowLength; i++){
//gets cells of current row
var oCells = oTable.rows.item(i).cells;
//gets amount of cells of current row
var cellLength = oCells.length;
//loops through each cell in current row
for(var j = 0; j < cellLength; j++){
// get your cell info here
var cellVal = oCells.item(j).innerHTML;
alert(cellVal);
}
}
</script>
我已经取得了进展的片段。 innerHTML似乎是空的这很奇怪,因为这是我在我的javascript代码中输入数据时添加数据的方式。 var cell4 = row.insertCell(3); cell4.innerHTML = limit; – MrM 2010-07-14 18:54:44
cell4.innerHTML = limit; 什么是限制? 我想如果你给cell4.innerHTML分配一个变量,你会得到那个单元格里的东西(假设你把所有东西都设置正确并且有实际的数据)。 – webdad3 2010-07-14 19:50:35
好的,我刚刚创建了一个测试页面并测试了脚本。我编辑了我的原始答案,以提醒我们表格中的每个单元格。从这里我认为你应该能够得到你所需要的东西。 – webdad3 2010-07-14 19:56:27