如何修复CSS切换背景颜色?

问题描述 投票:1回答:2

我试图通过使用按钮切换的javascript添加“rainbow”类来改变body的背景颜色。

我已经尝试使用我以前的代码来切换有效的类,我手动添加了类来检查我的CCS是否正确。我不确定问题的原因是什么。

<!DOCTYPE html>
<html>
<head>
<style>
    @keyframes bgcolor {
        0% {
            background-color: #45a3e5
        }

        30% {
            background-color: #66bf39
        }

        60% {
            background-color: #eb670f
        }

        90% {
            background-color: #f35
        }

        100% {
            background-color: #864cbf
        }
    }

    button {
        color: black;
        font-size: 48px;
        font-style: italic;
        font-family: "Comic Sans MS", cursive, sans-serif;
        text-align: center;
        padding: 20px;
        border-radius: 50px;
        background-color: gray;
    }

    .rainbow {
        -webkit-animation: bgcolor 20s infinite;
        animation: bgcolor 10s infinite;
        -webkit-animation-direction: alternate;
        animation-direction: alternate;
    }
</style>
<head>
<body id="body">
<button id="button">hello world</button>
<script>
function rainbowTime() {
        body.classList.toggle('rainbow');
    }
    button.onclick = rainbowTime;
</body>
</html>

当按下按钮时,背景应该改变颜色,就像我编辑html并添加“rainbow”类一样。如果我能得到任何帮助那就太棒了!

javascript html css html5
2个回答
1
投票

你有一些未封闭的标签。

head没有关闭。 script没有关闭。

每当你遇到JavaScript问题时。检查浏览器控制台以获取更多信息。

<!DOCTYPE html>
<html>

<head>
  <style>
    @keyframes bgcolor {
      0% {
        background-color: #45a3e5
      }
      30% {
        background-color: #66bf39
      }
      60% {
        background-color: #eb670f
      }
      90% {
        background-color: #f35
      }
      100% {
        background-color: #864cbf
      }
    }
    
    button {
      color: black;
      font-size: 48px;
      font-style: italic;
      font-family: "Comic Sans MS", cursive, sans-serif;
      text-align: center;
      padding: 20px;
      border-radius: 50px;
      background-color: gray;
    }
    
    .rainbow {
      -webkit-animation: bgcolor 20s infinite;
      animation: bgcolor 10s infinite;
      -webkit-animation-direction: alternate;
      animation-direction: alternate;
    }
  </style>

</head>

<body id="body">
  <button id="button">hello world</button>
  <script>
    function rainbowTime() {
      body.classList.toggle('rainbow');
    }
    button.onclick = rainbowTime;
  </script>
</body>

</html>

1
投票

在您的代码中,您没有关闭标记,您应该关闭head标记和脚本标记

 <!DOCTYPE html>
 <html>
<head>
<style>
   @keyframes bgcolor {
      0% {
          background-color: #45a3e5
      }

    30% {
        background-color: #66bf39
    }

    60% {
        background-color: #eb670f
    }

    90% {
        background-color: #f35
    }

    100% {
        background-color: #864cbf
    }
}

button {
    color: black;
    font-size: 48px;
    font-style: italic;
    font-family: "Comic Sans MS", cursive, sans-serif;
    text-align: center;
    padding: 20px;
    border-radius: 50px;
    background-color: gray;
}

.rainbow {
    -webkit-animation: bgcolor 20s infinite;
    animation: bgcolor 10s infinite;
    -webkit-animation-direction: alternate;
    animation-direction: alternate;
}
</style>
</head>
<body id="body">
<button id="button">hello world</button>
 </body>
  </html>
  <script>
    function rainbowTime() {
      body.classList.toggle('rainbow');
    }
     button.onclick = rainbowTime;
  </script>
© www.soinside.com 2019 - 2024. All rights reserved.