最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting column number from td - Stack Overflow

programmeradmin7浏览0评论

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.

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 sitting in to set col for:

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

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

-- solution for my specific problem was:

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

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

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

});

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.

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 sitting in to set col for:

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

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

-- solution for my specific problem was:

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

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

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

});
Share Improve this question edited Feb 15, 2011 at 14:09 asked Feb 15, 2011 at 13:20 user479053user479053 1
  • Documentation: api.jquery./index – Radek Commented Aug 17, 2012 at 13:22
Add a ment  | 

2 Answers 2

Reset to default 11

You can use the index() function.

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

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

 td.cellIndex 

demo

发布评论

评论列表(0)

  1. 暂无评论