jquery addClass 不适用于表格单元格
我需要一些帮助,
我正在尝试这样做
if(perc>0){
alert('change backgroundcolor and textcolor');
$('#stocktable tr td:last').addClass('stockhigher');
}
但是它在表格单元格上不起作用
我也尝试像这样设置选择器
$('#stocktable tr td:eq(2)).addClass...
$('#stocktable tr td.percentage').addClass...
!
它确实适用于桌子本身或桌子行,就像
$('#stocktable tr')
我在这里遗漏了什么一样?
谢谢,理查德
I need some help with this
I am trying to do this
if(perc>0){
alert('change backgroundcolor and textcolor');
$('#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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想到三件事:
:last
伪元素。这将最多匹配一个元素总数,在本例中是“stocktable”中的最后一个表格单元格。您可能指的是:last-child
吗?:eq(2)
,它将仅匹配整个集合中的第三个元素。您的意思可能是:nth-child(2)
吗?$("#stocktable tr td.eq(2)).addClass...
丢失并结束引号;并且为了进一步解释(1),假设您有一个包含 3 行、每行 4 个单元格且 id 为“mytable”的表格:
将为第三个单元格着色。第一行的 em> 元素(
:eq()
从零开始),而:每行中的第二单元格着色。
将为 为最后一行的最后一个单元格着色,但是:
将为每行中的最后一个单元格着色。
Three things spring to mind:
: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?: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; andTo further explain (1) imagine you have a table with 3 rows of 4 cells with an id of "mytable". This code:
will colour the third element of the first row (
:eq()
is zero-based) whereas:will colour the second cell in each row.
will colour the very last cell in the very last row but:
will colour the last cell in each row.