using jquery to find all td's in a table by column position
I am trying to add a class to the last cell in each row of a table...this does not work...it only applies the rightStyle to the last element in the first (top) row...
//the last cell in every row all have border right
var lastIndex = getLastVisibleIndex();
var rows = $("t开发者_StackOverflow社区able.scrollable tr");
rows.each(function () {
$("td:eq(" + lastIndex + ")").addClass(rightStyle)
});
Do it all in one line...
$('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/
I used the nth-child...
$("table.scrollable td:nth-child(" + lastIndex + ")").addClass(rightStyle);
Some good alternative solutions here though.
When you search for tds, you need to look only inside the current row. See the addition of this:
rows.each(function () {
$("td:eq(" + lastIndex + ")", this).addClass(rightStyle)
});
It's also possible that your table rows don't all have lastIndex
cells. Try this for more reliability:
rows.each(function () {
$(this).children("td").last().addClass(rightStyle)
});
$("table.scrollable tr").each(function () {
$(this).children("td").eq(lastIndex).addClass(rightStyle);
});
I do the same thing that you are doing, but I set the last td's column width
// SET FIRST AND LAST TD SIZE
$("tr").each(function() {
$(this).children("td:last").attr("width", 200);
});
精彩评论