如果...否则如果不会工作

If ...Else If wont work

它是这样工作的:从 问题: x==0||1 到 x==0||x==1 等等

这是来自 .html

的 运行
<body>   
<div class="" id="pimg1">

      </body><script>

      var x=new Date().getMonth();

     if (x == 0||x == 5||x == 2){document.getElementById('pimg1').className='pimg1';}
      else 
      if (x == 3||x == 4){document.getElementById('pimg1').className="pimg1a";}
      else 
      if (x == 6||x == 7||x == 8){document.getElementById('pimg1').className='pimg1b';}
      else                            {document.getElementById('pimg1').className='pimg1c';}


    </script></html>

外部 css:

.pimg1{
   background-image: url('images/style1.jpg');/*Zone 1*/}
  .pimg1a{
 background-image: url('images/style2.jpg');/*Zone 1*/}
 .pimg1b{
background-image: url('images/style3.jpg');/*Zone 1*/}
    .pimg1c{
background-image: url('images/style4.jpg');/*Zone 1*/}

首先,将 javascript 代码放在 <script></script> 标签之间,因为 javascript 代码不会 运行 在 html <div></div> 标签。

然后,使用 x == 0 || x == 9 || x == 2.

而不是 x == 0||9||2

请缩进您的代码以便于阅读。 || 运算符在 let 和右侧查找条件。 9 不是条件,2 也不是。当你输入 x == 9 || x == 2 时,你是说检查左边的条件,如果不正确则检查右边的条件。如果一个是真的,那么我们就可以开始了。

我们不能在 <script> 标签中使用 css 也不能从 <div> 标签中删除 <script> 标签,如下所示:

<html>

<head>
  <style>
    .pimg1 {
      background-image: url('images/style1.jpg');
      /*Zone 1*/
    }
    .pimg1a {
      background-image: url('images/style2.jpg');
      /*Zone 1*/
    }
    .pimg1b {
      background-image: url('images/style3.jpg');
      /*Zone 1*/
    }
    .pimg1c {
      background-image: url('images/style4.jpg');
      /*Zone 1*/
    }
  </style>
</head>

<body>
  <div class="" id="pimg1"></div>
  <script>
    var x = new Date().getMonth();
    if (x == 0 || 9 || 2) {
      document.getElementById('pimg1').className = 'pimg1';
    } else if (x == 3 || 5) {
      document.getElementById('pimg1').className = "pimg1a";
    } else if (x == 6 || 1 || 8) {
      document.getElementById('pimg1').className = 'pimg1b';
    } else {
      document.getElementById('pimg1').className = 'pimg1c';
    }
  </script>
</body>

</html>