正在尝试添加基于特定月份的类属性

Trying to add a class attribute based on a specific month

本文关键字:属性 于特定 添加      更新时间:2023-09-26

我有一个表设置,可以根据月份添加一个特定的类。我使用的是Javascript。现在,我似乎无法始终根据Javascript if语句添加类。我有以下代码:

var now = new Date();
      var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
      var month = now.getMonth();
      var date = now.getDate();
      var year = now.getFullYear();
      var hour = now.getHours();

     if (month == 7 && 8 && 9)
     $('.winter').addClass('TableRowColor');

     else if (month == 10 && 11 && 0)
     $('.spring').addClass('TableRowColor');

     else if (month == 1 && 2 && 3)
     $('.summer').addClass('TableRowColor');

     else if (month == 4 && 5 && 9);
     $('.fall').addClass('TableRowColor');
     {
}

我正在使用最后一个包含fall类的else-if语句来测试我的代码逻辑。由于我们现在是七月,而不是第4、5或9个月,我不认为秋季班会被申请,但现在仍然如此。我做错了什么?

当我去掉else if (month == 4 && 5 && 9);之后的分号时,类不会被应用。我做错了什么?

在最后一个else if的末尾有一个分号。这在你实际运行的代码中吗?说明:看起来fall类总是被添加的,因为它没有被包含在if语句中,因为分号放错了位置。