简体   繁体   English

Javascript:获取表单元格的内容(在div内)

[英]Javascript: get content of table's cell (inside div)

I have a little problem with finding cell's id (and its content). 我在查找单元格的ID(及其内容)时遇到了一些问题。 Main table contains multiple divisions and each division contains one table with several cells. 主表包含多个分区,每个分区包含一个包含多个单元格的表。 What I want is to display cell with id content_xx . 我想要的是显示ID为content_xx单元格。 My tables look like this: 我的表如下所示:

<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 for finding it: 查找功能:

function FindContent(id)
{
    t = document.getElementById("InfoTable").rows[0].cells.namedItem("content_"+id).innerHTML;
    alert(t);
    return;
}

I have onclick event which executes this function. 我有执行此功能的onclick事件。 It works for me only for first InfoTable , but it does not work when I try same thing on table #2 or further. 它仅对第一个InfoTable对我InfoTable ,但对表#2或更远的地方尝试相同的操作时不起作用。 I get this error: TypeError: document.getElementById(...).rows[0].cells.namedItem(...) is null . 我收到此错误: TypeError: document.getElementById(...).rows[0].cells.namedItem(...) is null How can I fix this and why it does not work? 我该如何解决这个问题,为什么它不起作用? Is it because of row[0] ? 是因为row[0]吗? If I change to row[1] , for example, then it doesn't work at all. 例如,如果我更改为row[1] ,那么它根本不起作用。 I can change structure of html but not completely. 我可以更改html的结构,但不能完全更改。

** Solved by icecub ** **由icecub解决**

The problem was that I was using same ID in divs, therefore, only first table would have been returned. 问题是我在div中使用了相同的ID,因此只返回了第一个表。 I just needed to use different ID. 我只需要使用其他ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM