0

我有这个 JS 代码块,可以在表格单元格中动态生成日期。

'<td class="dateStyleBlue" data="date"><span id = "' 
+ customers.ProductID + 'date">' + util.utility( customers.GetDate) 
+ '</span></td>'

当前的 CSS

.dateStyleBlue{
    color: blue;
    text-align:center;
    font-size: 10px;
}

新的 CSS 样式

.dateStyleRed{
    color: red;
    text-align:center;
    font-size: 10px;
}

如果日期已经过去,我想用新 CSS 替换当前 CSS 类。

所以这是我到目前为止所拥有的,

    var today = new Date();

    if (customer.GetDate < today) {
       $(dateStyleBlue).replaceWith(dateStyleRed);    
    }
4

2 回答 2

1

You should do a removeClass and a addClass.

 if (customer.GetDate < today) {
       $('.dateStyleBlue').removeClass('dateStyleBlue').addClass('dateStyleRed');    
 }

Don't forget to use the dot ( . ) to select a class and it need to be a string unless it's a javascript variable.

EDIT

You should be more specific with your selector because the code above will change every elements with the class dateStyleBlue

于 2013-07-24T00:03:59.703 回答
0

First I would give your another class to reference by, say "table_row".

Then when you want to switch classes use the following code.

$(".table_row").removeClass("dateStyleBlue");
$(".table_row").addClass("dateStyleRed");

Remember to use the dot to reference by class!

于 2013-07-23T23:59:22.077 回答