有没有办法让HTML5根据负值或正值改变文本颜色?

时间:2016-03-04 21:17:37

标签: c# html html5

我真的没有使用HTML 5的经验,并认为我会看到这是否可能。

无论如何,如果值为负,我可以在这里使用HTML5将文本颜色更改为红色吗?

现在尝试避免使用JavaScript或Jquery。

此代码的输出文件将转到HTML文件。

    vWriteFile.WriteLine("<tr>" + "<td>" + key + "</td>" + "<td>" + iSumOpen + "</td>" + "<td>" + iSumBuy + "</td>"
                + "<td>" + iSumSell + "</td>" + "<td>" + iSumSettleMM + "</td>" + "</tr>");

以下是整个方法:

    public void printToHTML(string vOutputPath)
    {
        StreamWriter vWriteFile = new StreamWriter(vOutputPath);
        vWriteFile.WriteLine("<table style=\"width:100%\">");
        vWriteFile.WriteLine("<tr>" + "<td>" + "<b>Account</b>" + "</td>" + "<td>" + "<b>Open</b>" + "</td>" + "<td>" + "<b>Buy</b>" + "</td>"
                + "<td>" + "<b>Sell</b>" + "</td>" + "<td>" + "<b>SettleMM</b>" + "</td>" + "</tr>");
        foreach (KeyValuePair<string, List<DataRecord>> kvp in vSummaryResults)
        {
            string key = kvp.Key; //assigns key
            List<DataRecord> list = kvp.Value;  //assigns value

            int iSumOpen = 0;
            int iSumBuy = 0;
            int iSumSell = 0;
            double iSumSettleMM = 0;

            foreach (DataRecord rec in list)
            {
                if (vSummaryResults.ContainsKey(key))
                {

                    iSumOpen += rec.open;
                    iSumBuy += rec.buy;
                    iSumSell += rec.sell;
                    iSumSettleMM += rec.settleMM;

                }

                else
                {
                    vSummaryResults.Add(key, list);
                }

            }
            Console.WriteLine(key + "\t" + iSumOpen + "\t" + iSumBuy + "\t" + iSumSell + "\t" + iSumSettleMM + "\t");
            Console.WriteLine("---------------------------------------------------------------------");

            //vWriteFile.WriteLine("{0},{1},{2},{3},{4}", key, iSumOpen, iSumBuy, iSumSell, iSumSettleMM);
            vWriteFile.WriteLine("<tr>" + "<td>" + key + "</td>" + "<td>" + iSumOpen + "</td>" + "<td>" + iSumBuy + "</td>"
                + "<td>" + iSumSell + "</td>" + "<td>" + iSumSettleMM + "</td>" + "</tr>");
        }
        vWriteFile.WriteLine("</table>");
        vWriteFile.Close();
    }

1 个答案:

答案 0 :(得分:0)

编辑:正如Nathan K在评论中指出的那样,你应该使用CSS类。我认为最好的方法是使用某种三元语句:

vWriteFile.WriteLine("<tr><td class=\"" + value < 0 ? "negative" : "positive" + "\">" + value + "</td></tr>");

可能也有效的旧答案:

您可以使用以下条件:

if(value < 0) {
    vWriteFile.WriteLine("<tr><td style=\"background-color:red;\">" + value + "</td></tr>");
}
else {
    vWriteFile.WriteLine("<tr><td>" + value + "</td></tr>");
}