简体   繁体   中英

Trying to add a class attribute based on a specific month

I have a table setup where I add a particular class based on the month. I'm using Javascript for this. Right now, I cannot seem to consistently get the class to be added based on my Javascript if statements. I have the following code:

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');
     {
}

I'm testing my code logic using the very last 'else if' statement that contains the 'fall' class. Since we are currently in the month of July, and NOT the 4th, 5th or 9th month, I wouldn't think .fall class would get applied, yet it still does. What am I doing wrong?

When I take away the semi colon after else if (month == 4 && 5 && 9); the class doesn't get applied. What am I doing wrong?

You have a semi colon at the end of that last else if. Is that in the code you're actually running? Explanation: looks like the fall class is always added because it's not enclosed in an if statement due to that misplaced semicolon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM