使用eq()获取基于索引的所有tds

时间:2012-04-27 22:50:49

标签: jquery jquery-selectors

我有索引2 - 即第3列,(0 - 1列,1 - 2列和2 - 3列),下面的选择器我只得到 1 td来自正确列,然而我希望所有索引2中的tds,我该如何实现?

$($(selector).find("td").eq(currentCol)).each(function(i) { 
  Console.log("Left-", currentCol,$(this).width() - diff);
  $(this).css("width", ($(this).width() + diff) + "px");
});

HTML:

<table style="width: 170mm; text-align: left;" class="te-tablerenderer"><tbody style="text-align: left;"><tr style="text-align: left;" class=""><td style="width: 64px;"><br></td>    <td style="">          <b>City:</b>        </td><td style="">          <b>City:</b>        </td><td style="">          <b>City:</b>        </td>     </tr><tr style="text-align: left;" class=""><td style="width: 64px;"><br></td>    <td style="">          <b>City:</b>        </td><td style="">          <b>City:</b>        </td><td style="">          <b>City:</b>        </td>     </tr><tr style="text-align: left;" class=""><td style="width: 64px;"><br></td>    <td style="">          <b>City:</b>        </td><td style="">          <b>City:</b>        </td><td style="">          <b>City:</b>        </td>     </tr></tbody>  </table>

2 个答案:

答案 0 :(得分:3)

你可以遍历每一行并抓住col位置/索引的td:

$(selector).find('tr').each(function(){
    var td = $(this).children('td').eq(2);
    //do some other stuff with the td
});

其中2是col位置/索引。

注意:通过阅读文档,使用.eq函数只会返回一个结果,因此现有代码只会影响一个元素。

答案 1 :(得分:2)

如何使用CSS选择器呢? nth-of-type应该做你想做的事:

$(selector).find("tr td:nth-of-type(3)")