0

例子:

<table>
  <tr>
     <th>Bacon</th>
     <th>Tuna</th>
     <th>Padas</th>
     <th>Another</th>
     <th>Name</th>
    </tr>
 </table>

页面加载后如何获取列位置

$(document).ready(function() { 
   bla..bla
});

输出将是bacon = 0, tuna = 1 padas = 2等等......

4

3 回答 3

1
$(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] + ',');

});

这里的例子:http : //jsfiddle.net/3Nzhu/1/

注意:你应该给你的表一个 id 并将上面的选择器更改为 '#mytableid tr th'

于 2013-04-03T08:37:19.703 回答
1

你可以使用map

var mapping = {};

$('table th').map(function(elem, index) {
    mapping[$(elem).text().toLowerCase()] = index;
});

尽管这完全取决于您要如何处理这些数据。

于 2013-04-03T08:38:29.967 回答
0
$(document).ready(function(){
    $counter = -1;
    $("table th").each(function(){
       document.write($(this).text() + " = " + ++$counter);
    });
});

请享用 :)

于 2013-04-03T08:39:08.603 回答