jquery addClass 不适用于表格单元格

发布于 2024-08-17 01:34:40 字数 457 浏览 4 评论 0原文

我需要一些帮助,

我正在尝试这样做

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亢潮 2024-08-24 01:34:40

我想到三件事:

  1. 您正在使用 :last 伪元素。这将最多匹配一个元素总数,在本例中是“stocktable”中的最后一个表格单元格。您可能指的是 :last-child 吗?
  2. 您正在使用 :eq(2) ,它将仅匹配整个集合中的第三个​​元素。您的意思可能是 :nth-child(2) 吗?
  3. $("#stocktable tr td.eq(2)).addClass... 丢失并结束引号;并且
  4. 您所做的事情没有任何问题。到底是什么不起作用?也许它不是可以应用于表格单元格的格式。

为了进一步解释(1),假设您有一个包含 3 行、每行 4 个单元格且 id 为“mytable”的表格:

$("#mytable td:eq(2)").css("background", "yellow");

将为第三个单元格着色。第一行的 em> 元素(:eq() 从零开始),而:

$("#mytable td:nth-child(2)").css("background", "yellow");

行中的第二单元格着色。

$("#mytable td:last").css("background", "yellow");

将为 为最后一行的最后一个单元格着色,但是:

$("#mytable td:last-child").css("background", "yellow");

将为行中的最后一个单元格着色。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文