我在查找单元格的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
适用于我,但是当我在表#2上或之后尝试相同的操作时,它不起作用。我收到此错误:TypeError: document.getElementById(...).rows[0].cells.namedItem(...) is null
。我该如何解决这个问题,为什么它不起作用?是因为row[0]
吗?例如,如果我更改为row[1]
,则根本不起作用。我可以更改html的结构,但不能完全更改。 最佳答案
**由icecub解决**
问题是我在div中使用了相同的ID,因此只返回了第一个表。我只需要使用其他ID。
关于javascript - Javascript:获取表单元格的内容(在div内),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29150892/