Uncaught TypeError: Cannot set property 'innerText' of null - For Countdown Script
Uncaught TypeError: Cannot set property 'innerText' of null - For Countdown Script
我收到 Uncaught TypeError: Cannot set 属性 'innerText' of null 控制台中的错误消息,其中错误一直在无限期上升。它与我对一组特定日期进行 运行 倒计时的脚本有关。
我已尝试删除可消除错误的脚本。除此之外,我不确定到底哪里出了问题。除了与 target.innerText = "Expired"
行有关的事情。
"use strict";
timer = function timer(target, date) {
var target = document.querySelector(target);
var countDownDate = new Date(date).getTime();
setInterval(function () {
var now = new Date().getTime();
var distance = countDownDate - now;
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor(distance % (1000 * 60 * 60 * 24) / (1000 * 60 * 60));
var minutes = Math.floor(distance % (1000 * 60 * 60) / (1000 * 60));
var seconds = Math.floor(distance % (1000 * 60) / 1000);
target.innerText = days;
if (distance < 0) {
target.innerText = "Expired";
}
}, 1000);
};
timer("#timer1", "Jan 01, 2019 11:59:59");
timer("#timer2", "Jan 02, 2019 11:59:59");
timer("#timer3", "Jan 03, 2019 11:59:59");
我们将不胜感激任何帮助和建议。
谢谢
编辑:
这是html
的一部分
<div class="container">
<div class="dates-container">
<div class="dates-wrapper">
<p class="date"><strong>Jan 01</strong> <span class="days-remaining"><span id="timer1"></span> Days Remaining</span></p>
<div class="divider"></div>
</div>
<div class="upcoming-dates">
<p class="date"><strong>Jan 02</strong> <span class="days-rem"> . <span id="timer2"></span> Days Remaining</span></p>
<div class="divider"></div>
</div>
</div>
</div>
它在主 index.html 上工作,我试图通过提供 "timerX" 的元素和 ID 将倒计时添加到不同的页面 - 直到我添加倒计时才出现错误到第二页。
再次感谢您的帮助,我觉得我忽略了一些简单但我不知道的东西。
行
var target = document.querySelector(target);
肯定是将 var target
设置为空。
所以这正是口译员告诉你的。
我假设它找不到 #timer3
。
我收到 Uncaught TypeError: Cannot set 属性 'innerText' of null 控制台中的错误消息,其中错误一直在无限期上升。它与我对一组特定日期进行 运行 倒计时的脚本有关。
我已尝试删除可消除错误的脚本。除此之外,我不确定到底哪里出了问题。除了与 target.innerText = "Expired"
行有关的事情。
"use strict";
timer = function timer(target, date) {
var target = document.querySelector(target);
var countDownDate = new Date(date).getTime();
setInterval(function () {
var now = new Date().getTime();
var distance = countDownDate - now;
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor(distance % (1000 * 60 * 60 * 24) / (1000 * 60 * 60));
var minutes = Math.floor(distance % (1000 * 60 * 60) / (1000 * 60));
var seconds = Math.floor(distance % (1000 * 60) / 1000);
target.innerText = days;
if (distance < 0) {
target.innerText = "Expired";
}
}, 1000);
};
timer("#timer1", "Jan 01, 2019 11:59:59");
timer("#timer2", "Jan 02, 2019 11:59:59");
timer("#timer3", "Jan 03, 2019 11:59:59");
我们将不胜感激任何帮助和建议。
谢谢
编辑:
这是html
的一部分<div class="container">
<div class="dates-container">
<div class="dates-wrapper">
<p class="date"><strong>Jan 01</strong> <span class="days-remaining"><span id="timer1"></span> Days Remaining</span></p>
<div class="divider"></div>
</div>
<div class="upcoming-dates">
<p class="date"><strong>Jan 02</strong> <span class="days-rem"> . <span id="timer2"></span> Days Remaining</span></p>
<div class="divider"></div>
</div>
</div>
</div>
它在主 index.html 上工作,我试图通过提供 "timerX" 的元素和 ID 将倒计时添加到不同的页面 - 直到我添加倒计时才出现错误到第二页。
再次感谢您的帮助,我觉得我忽略了一些简单但我不知道的东西。
行
var target = document.querySelector(target);
肯定是将 var target
设置为空。
所以这正是口译员告诉你的。
我假设它找不到 #timer3
。