这是表: -
var thetable = "";
thetable= thetable + "<tr><th>Audit ID</th><th>Date Modified</th><th>Auditor</th><th>Franchisee Name</th><th>Store ID</th><th>Template Name</th><th>Description</th></tr>";
如何使用javascript从特定列中提取数据? 我是新手,所以任何帮助将不胜感激:)
答案 0 :(得分:4)
看看这个demo
HTML
<table id="mytable" border="1">
<tr>
<th>Audit ID</th>
<th>Date Modified</th>
<th>Auditor</th>
</tr>
<tr>
<td id="colA_rowA">1</td>
<td id="colB_rowA">2</td>
<td id="colC_rowA">3</td>
</tr>
<tr>
<td id="colA_rowB">4</td>
<td id="colB_rowB">5</td>
<td id="colC_rowB">6</td>
</tr>
</table>
的JavaScript
// Option one by cell id
alert(document.getElementById("colB_rowA").innerHTML);
// Another option to run on all table
GetCellValues();
function GetCellValues() {
var table = document.getElementById('mytable');
for (var r = 0, n = table.rows.length; r < n; r++) {
for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
alert(table.rows[r].cells[c].innerHTML);
}
}
}
答案 1 :(得分:2)
innerHTML
是你的朋友。 ID为 mytable 的表格的示例代码段。
function GetCellValues() {
var table = document.getElementById('mytable');
for (var r = 0, n = table.rows.length; r < n; r++) {
for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
alert(table.rows[r].cells[c].innerHTML);
}
}
}