获取jQuery中的列位置

获取jQuery中的列位置,jquery,html-table,Jquery,Html Table,例如: <table> <tr> <th>Bacon</th> <th>Tuna</th> <th>Padas</th> <th>Another</th> <th>Name</th> </tr> </table> 输出将是培根=0,金枪鱼=1,帕达斯=2,依此类推

例如:

<table>
  <tr>
     <th>Bacon</th>
     <th>Tuna</th>
     <th>Padas</th>
     <th>Another</th>
     <th>Name</th>
    </tr>
 </table>
输出将是培根=0,金枪鱼=1,帕达斯=2,依此类推

$(函数(){
$(function() {
    var columns = new Array();
    $('table tr th').each(function(idx) {
        columns[idx] = $(this).text().toLowerCase();
    });

    // do what you need to do with your columns array here.  eg:
    for(var i = 0;i < columns.length;i++)
     document.write(columns[i] + ',');

});
var columns=新数组(); $('table tr th')。每个(函数(idx){ 列[idx]=$(this.text().toLowerCase(); }); //在此处对列数组执行所需操作。例如: 对于(var i=0;i
示例此处:


注意:您应该给您的表格一个id,并将上面的选择器更改为“#mytableid tr th”

您可以使用
映射

var mapping = {};

$('table th').map(function(elem, index) {
    mapping[$(elem).text().toLowerCase()] = index;
});
尽管这完全取决于您试图如何处理这些数据

$(document).ready(function(){
    $counter = -1;
    $("table th").each(function(){
       document.write($(this).text() + " = " + ++$counter);
    });
});

享受:)

虽然我已经修改了@Michael Nguyen的答案,但他明白了我想弄明白的,我来到这里,
column=[]$计数器=-1$(“表th”).each(函数(){column.push($(this.text()+”=“++++$counter);});警报(列)谢谢大家的想法。不用担心,如果你想要一个包含每个标题的数组,我不会介意创建一个新的解决方案:)
$(document).ready(function(){
    $counter = -1;
    $("table th").each(function(){
       document.write($(this).text() + " = " + ++$counter);
    });
});