English 中文(简体)
页: 1 彩票
原标题: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());

然而,我不敢以文字为基础确定一种地位。 所有“收割”的颜色都是红色的,而所有“帮助”的颜色都是绿色的。

有些人能够向我介绍如何实现这一目标?

哪怕是采取正确做法,使用 j,还是有更好的办法?

最佳回答

这项工作将:

$( .status:contains("Paid") ).css( color ,  red );
$( .status:contains("Received") ).css( color ,  green );

http://jsfiddle.net/MMEhc/

问题回答

而且,我也可以从......获得第一点的价值。

有了这名选任者,你就拿到了藏书。 页: 1

可通过所有<代码>status查询。 检查身份和改变肤色的囚室。

$( .status ).each(function() {
  // check text and do styling
});

但这并不可取(因为不需要)。 它损害了业绩。

http://api.jquery.com/contains-selector/"rel=“nofollow” http://api.jquery.com/contains-selector/

实际也不需要。 我认为,最佳选择业绩是明智的。

So the best option (IMHO) is:

为什么不给你在牢房中增加两个额外课程:status-receivedstatus- paid<>。 既然你已经知道这些囚室的地位,就让他们知道。

因此,你可以做到:

$( .status-received ).css("color", "red");
$( .status-paid ).css("color", "red");

或者完全放弃 j(因为我们不需要它再做任何事情(除非我们会积极改变囚室),而两门的风格是使用安全局。

如果你从一个数据库中印刷表格,则仅根据结果向<代码>td分配一个类别。

然后将背景说明划入这一类别。

td.paid {
 display:block;
background-color:red;

}

td.recieved {
 display:block;
background-color:green;
}

为什么你们首先需要为此使用 j子?

如果我没有错的话,上述法典就是 j,如果你使用的话,不会忘记增加李比。

我最近不得不这样做。 当发生错误时,我需要改变背景颜色,以红色(well,粉碎),并在一切都好时留下。 我的守则如下:

warning_color = "#ffffff";

if (error_happened)
    warning_color = "#ffaaaa";

$("#input_box").css( background-color , warning_color);
$( td.status ).each(function() {
  if ($(this).text() ==  Received ) {
      $(this).style( color ,  red );
  } // similarly for other statuses
});
$( .status ).addClass($(".status").html());

然后,你可以有<代码>。 Paid category and a . 收受级,并贴现这些类别中的标记。 如果你想改变这些 c子的话,就会从小script中抽取。 另外,如果需要,也可以在其他地点使用这些课程。





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!