未捕获的类型错误:无法读取 null 的属性(读取“querySelector”)

我试图让 this countdown 在我的本地工作,但在浏览器中查看它时不断收到此错误。

未捕获的类型错误:无法读取 null 的属性(读取“querySelector”)

我尝试将脚本标签移动到 html 文件的底部并确认 CSS 选择器。

非常感谢任何帮助。

This is a screenshot of the error in Chrome

scripts.js

function getTimeRemaining(endtime) {
  var t = Date.parse(endtime) - Date.parse(new Date());
  var seconds = Math.floor((t / 1000) % 60);
  var minutes = Math.floor((t / 1000 / 60) % 60);
  var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
  var days = Math.floor(t / (1000 * 60 * 60 * 24));
  return {
    total: t,
    days: days,
    hours: hours,
    minutes: minutes,
    seconds: seconds
  };
}

function initializeClock(id, endtime) {
  var clock = document.getElementById(id);
  var daysSpan = clock.querySelector(".days")
  var hoursSpan = clock.querySelector(".hours");
  var minutesSpan = clock.querySelector(".minutes");
  var secondsSpan = clock.querySelector(".seconds");

  function updateClock() {
    var t = getTimeRemaining(endtime);

    if (t.total <= 0) {
      clearInterval(timeinterval);

      var newTime = Date.parse(endtime);
      var nowTime = Date.parse(new Date());

      while (newTime <= nowTime) {
        newTime = newTime + 1 * 24 * 60 * 60 * 1000; // add 24hours
      }

      var deadline = new Date(newTime);
      initializeClock('countdown', deadline);
    } else {
      daysSpan.innerHTML = t.days;
      hoursSpan.innerHTML = ("0" + t.hours).slice(-2);
      minutesSpan.innerHTML = ("0" + t.minutes).slice(-2);
      secondsSpan.innerHTML = ("0" + t.seconds).slice(-2);
    }
  }

  updateClock();
  var timeinterval = setInterval(updateClock, 1000);
}

var deadline = "December 31 2030 16:00:00 GMT+1100";
initializeClock("countdown", deadline);

index.html

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<script src="scripts.js"></script>
  <div id="countdown-parent"><h5 class="countdown-title">Order before 4pm for same day delivery</h5>
  <div id="countdown" class="countdown">
    <div class="countdown-number">
      <span class="days"></span>
      <span class="countdown-text">Days</span>
    </div>
    <div class="countdown-number">
      <span class="hours countdown-time"></span>
      <span class="countdown-text">Hours</span>
    </div>
    <div class="countdown-number">
      <span class="minutes countdown-time"></span>
      <span class="countdown-text">Minutes</span>
    </div>
    <div class="countdown-number">
      <span class="seconds countdown-time"></span>
      <span class="countdown-text">Seconds</span>
    </div>
  </div>
  </div>
</body>
</html>
stack overflow Uncaught TypeError: Cannot read properties of null (reading 'querySelector')
原文答案
author avatar

接受的答案

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
  <div id="countdown-parent"><h5 class="countdown-title">Order before 4pm for same day delivery</h5>
  <div id="countdown" class="countdown">
    <div class="countdown-number">
      <span class="days"></span>
      <span class="countdown-text">Days</span>
    </div>
    <div class="countdown-number">
      <span class="hours countdown-time"></span>
      <span class="countdown-text">Hours</span>
    </div>
    <div class="countdown-number">
      <span class="minutes countdown-time"></span>
      <span class="countdown-text">Minutes</span>
    </div>
    <div class="countdown-number">
      <span class="seconds countdown-time"></span>
      <span class="countdown-text">Seconds</span>
    </div>
  </div>
  </div>
</body>
<script src="scripts.js"></script>
</html>

答案:

相关问题