开发者

jquery addClass not working on tablecell

开发者 https://www.devze.com 2022-12-15 01:15 出处:网络
I need some help with this I am trying to do this if(perc>0){ alert(\'change backgroundcolor and tex开发者_JAVA技巧tcolor\');

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:

  1. 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?
  2. You're using :eq(2) which will match the third element in the entire set only. Do you perhaps mean :nth-child(2)?
  3. $("#stocktable tr td.eq(2)).addClass... is missing and end quote; and
  4. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号