我想从 html 表中提取数据,例如
<table>
<tr>
<th> Header1 </th>
<th> Header2 </th>
<th> Header3 </th>
</tr>
<tr>
<td> Value 1,1 </td>
<td> Value 2,1 </td>
<td> Value 3,1 </td>
</tr>
... rows ...
</table>
并获取数组:
我如何使用 jQuery 做到这一点?
我不想序列化它,也不想将它放入 JSON 对象中,因为我想用它来呈现图表。
相关一般设计问题:
目前我有类似的东西
1. ajax query returns html table
2. use jQuery to get values from html table
3. render chart
从 ajax 查询中抛出一个 JSON 对象,然后从那里呈现一个表格和图表是否更有意义?
最佳答案
演示已更新 http://jsfiddle.net/ish1301/cnsnk/
var header = Array();
$("table tr th").each(function(i, v){
header[i] = $(this).text();
})
alert(header);
var data = Array();
$("table tr").each(function(i, v){
data[i] = Array();
$(this).children('td').each(function(ii, vv){
data[i][ii] = $(this).text();
});
})
alert(data);
关于javascript - 通过 jQuery 将 HTML 表格数据转换为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4349952/