如何用javascript获取表格中一行的值

如题所述

1、简单的,表格中的一行添加id属性

var item=document.getElementById("id");

2、在行本身绑定方法将自身作为参数传递

<html>
<head>
<script type = 'text/javascript'>
var curRow; //全局行号
var curRowId; //选中行的记录信息的ID
var curColor;
function selectRow(tr){//tr行本身
curRow = tr;
curRowId = tr.id;
alert(tr.cells[0].innerText);
}
</script>
</head>
<body onload = "javascript:selectRow(1)">
<table border = "1 solid">
<tr onclick = "selectRow(this);">
<td>001</td>
<td>google</td>
</tr>
<tr onclick = "selectRow(this);">
<td>002</td>
<td>baidu</td>
</tr>
</table>
</body>
</html>

3、获取所有行集合,然后通过下标访问某一行

var items=document.getElementByTagName("tr");
//获取某一行(下标从0开始,到items.length-1结束)
var hang=items[0];

温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-11-05
假设table的id是tab1,你要获取第2行,第3列TD的内容

alert(document.getElementById('tab1').rows[1].cells[2].innerHTML);

具体可见DHTML参考手册CHM版,搜索table

参考资料:http://blog.everalan.com/80.html

第2个回答  2009-11-05
给那一行一个特殊的id 然后javascript引用 这个方法最方便本回答被提问者采纳
第3个回答  2009-11-05
在<tr>中定义一个ID
用var *** = document.getElementsById("ID");
就可以取到。
第4个回答  2009-11-05
百度一下“javascript 遍历table”
相似回答