[英]Javascript: get content of table's cell (inside div)
我在查找单元格的ID(及其内容)时遇到了一些问题。 主表包含多个分区,每个分区包含一个包含多个单元格的表。 我想要的是显示ID为content_xx
单元格。 我的表如下所示:
<table id="MainTable"> // Main (global) table
<div id="divid_1"> // First div in main table
<table id="InfoTable"> // First div's table
<td id="title_10">CellTitle</td> // Cell #1
<td id="content_11">CellContent</td> // Cell #2
<div id="divid_2"> // Second div in main table
<table id="InfoTable"> // Second div's table
<td id="title_20">CellTitle</td> // Cell #1
<td id="content_21">CellContent</td> // Cell #2
// etc...
</table>
查找功能:
function FindContent(id)
{
t = document.getElementById("InfoTable").rows[0].cells.namedItem("content_"+id).innerHTML;
alert(t);
return;
}
我有执行此功能的onclick事件。 它仅对第一个InfoTable
对我InfoTable
,但对表#2或更远的地方尝试相同的操作时不起作用。 我收到此错误: TypeError: document.getElementById(...).rows[0].cells.namedItem(...) is null
。 我该如何解决这个问题,为什么它不起作用? 是因为row[0]
吗? 例如,如果我更改为row[1]
,那么它根本不起作用。 我可以更改html的结构,但不能完全更改。
**由icecub解决**
问题是我在div中使用了相同的ID,因此只返回了第一个表。 我只需要使用其他ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.