我想让我的javascript告诉我这个地方是开放还是关闭,但它无法正常工作

时间:2017-04-19 18:46:20

标签: javascript html

我对javascript很新并且遇到了我的问题,这个地方在不同月份开放不同,所以我希望能够阅读这个月,然后告诉我它是否开放。

<script type="text/javascript">
    var time = newDate().getHours();
    var opening;
    var month = newDate().getMonth();

    if (month == 0) {
      if (time <= 17) {
        opening = "Open";
      }
      else {
        opening = "closed";
      }
    }
    else if (month == 1) {
      if (time <=18) {
        opening = "Open";
      }
      else {
        opening = "closed";
      }
    }
    else if (month = 3) {
      if (time <=19) {
        opening = "Open";
      }
      else {
        opening = "closed";
      }
    }
    else if (month = 4) {
      if (time <=19) {
        opening = "Open";
      }
      else {
        opening = "closed";
      }
    }
    else if (month = 5) {
      if (time <=19) {
        opening = "Open";
      }
      else {
        opening = "closed";
      }
    }
    else if (month = 6 ) {
      if (time <=21) {
        opening = "Open";
      }
      else {
        opening = "closed";
      }
    }
    else if (month = 7) {
      if (time <=19) {
        opening = "Open";
      }
      else {
        opening = "closed";
      }
    }
    else if (month = 8) {
      if (time <=19) {
        opening = "Open";
      }
      else {
        opening = "closed";
      }
    }
    else if (month = 9) {
      if (time <=19) {
        opening = "Open";
      }
      else {
        opening = "closed";
      }
    }
    else if (month = 10) {
      if (time <=19) {
        opening = "Open";
      }
      else {
        opening = "closed";
      }
    }
    else (month = 11) {
      if (time <=19) {
        opening = "Open";
      }
      else {
        opening = "closed";
      }
    }

    document.getElementById("demo").innerHTML = opening;
</script>

我查了一些例子,但似乎无法得到它

2 个答案:

答案 0 :(得分:0)

将所有值从if(month = 3)更改为if(month == 3),将3更改为11。它会工作。你错过了month == 2的条件。

首选swich()用于此类案件。它的mich更干净

答案 1 :(得分:0)

一些问题:

    第1行和第3行中的
  • newDate()应为new Date()
  • 使用==进行比较而不是=,您在大多数地方都错过了它:注意else if (month = 3) {else if (month == 3) {
  • 您的上一个else应该是else if,因为您已在其中设置条件:else if (month == 11) {