javascript - 通过 jQuery 将 HTML 表格数据转换为数组

标签 javascript jquery highcharts

我想从 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/

    相关文章:

    javascript - 仅在不包含文本 JQuery 时附加 div

    javascript - Highcharts:隐藏和显示图例

    Highcharts - 仅在需要时显示小数位

    javascript - 替换检测空对象的polyfill

    javascript - 如何使用 jQuery 显示/隐藏 DIV

    javascript - 控制jquery模态对话框灰色部分的类名

    javascript - 如何将 jQuery DOM 对象附加到另一个对象

    jquery - 您会在 Rails 应用程序中使用什么来自动完成?

    javascript - "fadeTo"当我滚动图像太快时似乎有一个错误

    javascript - 更新 'stacked columns' 图表 : update() or setData()? 上的 highcharts.js 数据