我正在尝试访问<td>
如下:
<table class="datadisplaytable" summary="This table lists the scheduled meeting times and assigned instructors for this class..">
<caption class="captiontext">Scheduled Meeting Times</caption>
<tbody><tr>
<th class="ddheader" scope="col">Time</th>
<th class="ddheader" scope="col">Days</th>
<th class="ddheader" scope="col">Where</th>
<th class="ddheader" scope="col">Date Range</th>
<th class="ddheader" scope="col">Schedule Type</th>
<th class="ddheader" scope="col">Instructors</th>
</tr>
<tr>
<td class="dddefault">8:35 AM - 9:55 AM</td>
<td class="dddefault">TR</td>
<td class="dddefault">Trottier Building 0100</td>
<td class="dddefault">Jan 05, 2015 - Apr 13, 2015</td>
<td class="dddefault">Lecture</td>
<td class="dddefault">Harry Leib </td>
</tr>
</tbody></table>
我在一个名为 var data_tables
的 HTML 集合中有一堆这样的表。我可以使用 data_tables[0].childNodes[0].innerText
成功访问标题(即集合中的第一个表)。但是,当我尝试访问<td>
时的(例如文本“8:35 AM - 9:55 AM”)和 data_tables[0].childNodes[1].childNodes[1].childNodes[0].innerText
使用 Chrome 时出现“未定义”。我缺少什么?是<td>
从<table>
开始不是三层节点?
最佳答案
您可能会获取文本节点而不是 td
元素。
不要将.childNodes
与表一起使用。请改用 .tBodies
、.rows
和 .cells
集合。
data_tables[0].tBodies[0].rows[1].cells[0].innerText
此外,我会使用 .textContent
而不是 .innerText
,以便您使用标准属性。
关于javascript - 使用 javascript 访问表数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28789622/