从td获取列号

nul*_*ull 10 javascript jquery tablesorter

tablesorter编写扩展名虽然是我第一次尝试扩展任何js.我有一些<select>s并且<td>需要知道这个td所在的列.

当在这些选择中的任何一个中改变值时,例如

$('select').change(function(){

});
Run Code Online (Sandbox Code Playgroud)

我需要得到这个选择是列保持坐着的设置col为:

('tr.result > td:nth-child('+col+')').each(function(){
Run Code Online (Sandbox Code Playgroud)

有没有办法可以从td选择中得到这个?!?

- 针对我的具体问题的解决方案是:

$('select').change(function(){

    td  = $(this).parent('td');

    col = $(td).parent().children().index(td);

});
Run Code Online (Sandbox Code Playgroud)

ReF*_*cus 9

你可以使用这个index()功能.

col = $(this).parent().children().index($(this));
Run Code Online (Sandbox Code Playgroud)


小智 5

cellIndex属性返回表格行的单元格集合中单元格的位置. W3Schools的

 td.cellIndex 
Run Code Online (Sandbox Code Playgroud)

演示

  • 对于使用“colspan”的表,“cellIndex”已损坏。这意味着“cellIndex”可能会为彼此重叠的单元格返回不同的值。 (2认同)