<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>网页特效|Linkweb.cn/Js|---访问表格的每个TD</title>
</head>
<script>
function showText(){
var oTbody=table1.children.item(0);
for(var i=0;i<oTbody.children.length;i++)
{
var oTR=oTbody.children.item(i);
for(var j=0;j<oTR.children.length;j++)
text1.value+="第"+(i+1)+"行;"+"第"+(j+1)+"列的内容是:"+oTR.children.item(j).innerHTML+"\n";
}
}
</script>
<body>
<table border="1" id="table1">
<tr>
<td>safssd</td>
<td>asfdasdf</td>
<td>asfasdfsafsad</td>
</tr>
<tr>
<td>asdfafsda</td>
<td>sdfasfasdsadf</td>
<td>sdfasdfasdfasfd</td>
</tr>
</table>
<button onclick="showText()">showText</button>
<textarea id="text1" rows="10" cols="100"></textarea>
</body>
</html>
本文介绍了一个JavaScript函数showText,该函数能够遍历HTML表格中的所有单元格(TD元素),并获取其内容。通过两个嵌套的for循环实现对表格每一行(TR)和每一列(TD)的访问,并将信息汇总到一个文本区域中展示。
160

被折叠的 条评论
为什么被折叠?



