表格单元格中文本颜色的条件更改 [英] Conditional change of text colour inside table cell
本文介绍了表格单元格中文本颜色的条件更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用jQuery来有条件地更改某些表格单元格的颜色.我有一张桌子:
I'm try to use jQuery to conditionally change the color of some table cells. I have a table:
<tr>
<td>Class Code</td>
<td class="qty">3</td>
<td>Science</td>
</tr>
<td>Class Code</td>
<td class="qty">4</td>
<td>Science</td>
</tr>
. . .
lowqty {color:red;}样式定义
A style definition lowqty{color:red;}
当数字低于4
时,我想更改第二列的颜色.
我正在尝试通过以下方法做到这一点,但还没有成功.
I want to change the color of the second column when the number is below 4
.
I'm attempting to do that with the following, but haven't got it to work.
$('.qty').change(
function() {
if ($(this).text() <= 3 ) {
$(this).addClass('lowqty');
}
});
我尝试将3
用作字符串和数字,并且尝试过滤并添加除change之外的内容.
I've tried 3
as a string as well as a number, and I've tried filter and add in addition to change.
推荐答案
$('.qty').each(function() {
if (parseInt($(this).text()) < 4) {
$(this).addClass('lowqty');
}
});
这应该可以解决问题.另外,您还缺少一个开头的TR标签:
That should do the trick. Also you're missing an opening TR tag:
这篇关于表格单元格中文本颜色的条件更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文