jquery选择列的最后一个单元格

时间:2011-02-21 21:24:53

标签: jquery jquery-selectors

我正在寻找一种从表格列中选择最后一个单元格的方法 此列有一个“.B89”类,这是我试图从中选择的。

这是我尝试过的,到目前为止,但它没有用。

$('table#incometable th.B89:last-child').html("data to show!!!");

该表是动态创建的,因此我将做一个简化版本:

<table id="incometable"> 
    <tr>
        <th class="B89">Dude 1</th> 
        <th class="B55">Dude 2</th> 
        <th class="B78">Dude 3</th> 
    </tr>
    <tr>
        <td>float values</td> 
        <td>float values</td> 
        <td>float values</td> 
    </tr>
    <tr>
        <td>float values</td> 
        <td>float values</td> 
        <td>float values</td> 
    </tr>
    <tr>
        <td>float values</td> 
        <td>float values</td> 
        <td>float values</td> 
    </tr>
    <tr>
        <td>---This Cell needs to be modified---</td> 
        <td></td> 
        <td></td> 
    </tr>
</table>

2 个答案:

答案 0 :(得分:3)

如果没有看到你的标记就说不出来,但我觉得你可能试图用标题<th class='B89'>来引用最后一列。

如果是这样,您可以从该单元格中获取.index(),然后选择最后一行并在该索引处获取<td>

  //---v----------make sure the DOM is loaded
$(function() {
    var table = $('#incometable');
    var idx = table.find('th.B89').index();

    table.find('tr:last > td').eq( idx ).html("data to show!!!");
});
  • 缓存$('#incometable')选项。

  • find()(docs) th.B89

  • 获取th.B89

  • index()(docs)
  • 获取:last行的<td>元素,然后使用eq()(docs)方法选择具有相同索引的元素。

答案 1 :(得分:0)

你需要:last-child的最后一个选择器,因为你想要在collumn中的最后一个单元格而不是行。

TH也应该在第一行,所以你可能会搜索一个td。

在不知道你的表格的情况下仍然难以分辨。

$('table#incometable td.B89:last')
相关问题