使用jquery按列位置查找表中的所有td

时间:2011-10-05 16:07:29

标签: jquery each

我正在尝试将一个类添加到表的每一行中的最后一个单元格...这不起作用...它只将rightStyle应用于第一行(顶部)行中的最后一个元素... < / p>

//the last cell in every row all have border right
                var lastIndex = getLastVisibleIndex();
                var rows = $("table.scrollable tr");
                rows.each(function () {
                    $("td:eq(" + lastIndex + ")").addClass(rightStyle)
                });

6 个答案:

答案 0 :(得分:13)

在一行中完成所有工作......

$('table tr td:last-child').addClass(rightStyle);

// Targeting a particular column as pointed out by FiveTools
// Note that :nth-child(n) is 1-indexed
$('table tr td:nth-child(3)').addClass('highlight');

http://jsfiddle.net/cobblers/hWqBU/

答案 1 :(得分:2)

我用了nth-child ......

   $("table.scrollable td:nth-child(" + lastIndex + ")").addClass(rightStyle);

虽然这里有一些很好的替代解决方案。

答案 2 :(得分:1)

搜索tds时,只需查看当前行内部。请参阅添加

            rows.each(function () {
                $("td:eq(" + lastIndex + ")", this).addClass(rightStyle)
            });

答案 3 :(得分:0)

您的表行也可能没有lastIndex个单元格。试试这个以获得更高的可靠性:

rows.each(function () {
    $(this).children("td").last().addClass(rightStyle)
});

答案 4 :(得分:0)

$("table.scrollable tr").each(function () {
                    $(this).children("td").eq(lastIndex).addClass(rightStyle);
                });

答案 5 :(得分:0)

我做的是你正在做的事情,但我设置了最后一个td的列宽

// SET FIRST AND LAST TD SIZE
$("tr").each(function() {
    $(this).children("td:last").attr("width", 200);
});
相关问题