jquery addClass not working on tablecell
I need some help with this
I am trying to do this
if(perc>0){
alert('change backgroundcolor and tex开发者_JAVA技巧tcolor');
$('#stocktable tr td:last').addClass('stockhigher');
}
but It does not work on a tablecell
I also try'd to set the selector like this
$('#stocktable tr td:eq(2)).addClass...
$('#stocktable tr td.percentage').addClass...
nothing!
it does work on the table itself or a tablerow like
$('#stocktable tr')
am I missing something here?
thanks, Richard
Three things spring to mind:
- You're using the
:last
pseudo-element. That will match at most one element total, in this case the very last table cell in "stocktable". Do you perhaps mean:last-child
instead? - You're using
:eq(2)
which will match the third element in the entire set only. Do you perhaps mean:nth-child(2)
? $("#stocktable tr td.eq(2)).addClass...
is missing and end quote; and- There is nothing wrong with what you're doing. What precisely isn't working? Perhaps it's not formatting that can be applied to a table cell.
To further explain (1) imagine you have a table with 3 rows of 4 cells with an id of "mytable". This code:
$("#mytable td:eq(2)").css("background", "yellow");
will colour the third element of the first row (:eq()
is zero-based) whereas:
$("#mytable td:nth-child(2)").css("background", "yellow");
will colour the second cell in each row.
$("#mytable td:last").css("background", "yellow");
will colour the very last cell in the very last row but:
$("#mytable td:last-child").css("background", "yellow");
will colour the last cell in each row.
精彩评论