注册 登录
编程论坛 JavaScript论坛

js table 问题

贾敏 发布于 2011-01-18 19:31, 451 次点击
程序代码:
<table style="position:absolute; left: 309px; top: 506px; height: 343px; width: 339px;" width="350" height="304" border="5" cellpadding="1" cellspacing="1" bgcolor="#CCCCCC" id="biaoge" >
  
   <tr bordercolor="#003333">
     <th width="116" height="54" bgcolor="#CCCCCC" id="jin" scope="row"  >1</th>
     <td width="213" bgcolor="#CCCCCC">aa</td>
   </tr>
   <tr bordercolor="#003333">
     <th height="31" bgcolor="#CCCCCC" scope="row">2</th>
     <td bgcolor="#CCCCCC"></td>
   </tr>
   <tr bordercolor="#003333">
     <th height="29" bgcolor="#CCCCCC" scope="row">3</th>
     <td bgcolor="#CCCCCC"></td>
   </tr>

 </table><body>
<input type="button" name="getTableContent" value="获得表格内容" onclick="getTableContent(rowIndex.value)" />

 </body> <script>
function getTableContent(rowIndex) {
                var node = document.getElementsByTagName("table")[0];//获取第一个表格
                var child = node.getElementsByTagName("tr")[rowIndex-1];//获取行的第一个单元格
                //var text = child.firstChild.innerHTML;
                var text = child.firstChild.innerHTML;
                window.alert("表格第" + rowIndex + "行的内容为: " + text);
        }

   </script>
如上述代码获取第一个表格行的第一个单元格的内容。如果我想获取第一个表格行的第二个单元格内容怎么办?
2 回复
#2
aspic2011-01-19 10:37
程序代码:
function getTableContent(rowIndex) {
    var node = document.getElementsByTagName("table")[0];
    var child = node.getElementsByTagName("tr")[rowIndex-1];
    var text = child.getElementsByTagName("td")[0].innerHTML;
    alert(text);
}
#3
aspic2011-01-19 10:37
rowIndex=1
1