1

我正在尝试在我的网页上一段时间(1700 小时)后将背景和一 (1) 个标题更改为不同的颜色。但是,它不会触发。我在这里做错了什么?

var today = new Date();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dayLight = 0900;
var lightMode = 1700;


if (time >= dayLight && time <= lightMode) {
    document.body.style.backgroundColor = "white";
    document.querySelector("h1.example").style.color = "black";
    console.log(time + " " + "=" + " " + "Light Mode!");
} else {
    document.body.style.backgroundColor = "black";
    document.querySelector("h1.example").style.color = "white";
    console.log(time + " " + "=" + " " + "Dark Mode!");
}
<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <title>Dark Mode Example</title>
    </head>

    <body>
       
        <h1 class="example">Dark Mode Example</h1>
   
        <p>Test Paragraph</p>
    </body>
    <script src="script.js"></script>

</html>

4

1 回答 1

0

//I just set an interval for every second to check the time again
var s=setInterval(()=>{
  var today = new Date();
  var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
  var dayLight = 0900;
  var lightMode = 1700;


  if (time >= dayLight && time <= lightMode) {
      document.body.style.backgroundColor = "white";
      document.querySelector("h1.example").style.color = "black";
      //console.log(time + " " + "=" + " " + "Light Mode!");
  } else {
      document.body.style.backgroundColor = "black";
      document.querySelector("h1.example").style.color = "white";
      //console.log(time + " " + "=" + " " + "Dark Mode!");
  }
},1000)
<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <title>Dark Mode Example</title>
    </head>

    <body>
       
        <h1 class="example">Dark Mode Example</h1>
   
        <p>Test Paragraph</p>
    </body>
    <script src="script.js"></script>

</html>

于 2020-12-02T00:17:45.567 回答