jQuery更改表单元格的文本颜色基于文本 [英] jQuery change table cell text color based on text
问题描述
我有一个表,其中有包含状态的数据列。两个示例状态是拒绝和付费
我想要做的是将被拒绝的文本颜色更改为红色, 付费为绿色。
对于具有此状态的单元格,我向 td
:
< td class =status>
@ Html.DisplayFor(modelItem => item.Status)
< / td>
所以我可以很容易地识别它。
我发现我可以改变所有状态的颜色为红色使用:
$('。status')。css color,red);
也可以通过以下方式获取第一个的值:
alert($('。status')。html());
但是我不确定如何根据文本设置状态颜色。
有人可以告诉我如何实现这一目标?
我还是使用jQuery的正确方法,还是有更好的css方式?
这将工作:
$('status:contains(Paid)')。css ','red');
$('。status:contains(Received)')。css('color','green');
I have a table which has columns of data that contain status. Two example statuses would be "Rejected" and "Paid"
What I'm wanting to do is changed the text color of the "Rejected" to red and the color of the "Paid" to green.
For the cells that have this status I added a classs to the td
like:
<td class="status">
@Html.DisplayFor(modelItem => item.Status)
</td>
So I can easily identify it.
I found I could change the color of all the statuses to Red using:
$('.status').css("color", "red");
Also I could get the value of the first one by:
alert($('.status').html());
However I'm unsure how to set a status color based on its text. ie for all "Received" set color to red and for all "Paid" set color to green.
Can somebody enlighten me on how to achieve this?
Am I even following the right approach using jQuery or is there a better css way?
This will work:
$('.status:contains("Paid")').css('color', 'red');
$('.status:contains("Received")').css('color', 'green');
这篇关于jQuery更改表单元格的文本颜色基于文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!