javascript 从 td 获取列号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5004146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:37:49  来源:igfitidea点击:

Getting column number from td

javascriptjquerytablesorter

提问by null

Writing an extension for tablesorter.. though its my first attempt at extending any js. I have a number of <select>s within a row of <td>s & need to know the column this td sits in.

tablesorter编写扩展......尽管这是我第一次尝试扩展任何 js。我在<select>s 的一行中有多个s 并且<td>需要知道这个 td 所在的列。

When a value is changed in any of these selects e.g.

当这些选择中的任何一个中的值发生更改时,例如

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

});

I need to get hold of the column this select is sittingin to set colfor:

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

('tr.result > td:nth-child('+col+')').each(function(){

Is there a way I can get this from the td select is in?!?

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

-- solution for my specific problem was:

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

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

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

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

});

采纳答案by ReFocus

You can use the index()function.

您可以使用该index()功能。

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

回答by ReFocus

The cellIndex property returns the position of a cell in the cells collection of a table row. w3schools

cellIndex 属性返回单元格在表格行的单元格集合中的位置。 w3schools

 td.cellIndex 

demo

演示