gpt4 book ai didi

javascript - 出现错误 : Cannot set property 'innerHTML' of null

转载 作者:行者123 更新时间:2023-12-03 11:47:54 25 4
gpt4 key购买 nike

我不断收到错误消息:无法将属性“innerHTML”设置为 null。

我尝试更改innerHTML ->innerText。仍然无法工作。

我在这里缺少什么???

  function countdown(minutes) {
var seconds = 60;
var mins = minutes
function tick() {
var counter = document.getElementById("timer");
var current_minutes = mins-1;
seconds--;
counter.innerHTML =
current_minutes.toString() + ":" + (seconds < 10 ? "0" : "") + String(seconds);
if( seconds > 0 ) {
setTimeout(tick, 1000);
} else {
if(mins > 1){
// countdown(mins-1); never reach “00″ issue solved:Contributed by
Victor Streithorst
setTimeout(function () { countdown(mins - 1); }, 1000);
}
}
}
tick();
}
countdown(3);

html

   <div id="timer"></div>

最佳答案

您正在调用 countdown(),它又调用 tick(),后者又调用 document.getElementById("timer"),< em>在该元素被解析之前。

尝试这样做:

<div id="timer"></div>
<script type="text/javascript">
function countdown(minutes) {
var seconds = 60;
var mins = minutes
function tick() {
var counter = document.getElementById("timer");
var current_minutes = mins-1
seconds--;
counter.innerHTML = current_minutes.toString() + ":" + (seconds < 10 ? "0" : "") + String(seconds);
if( seconds > 0 ) {
setTimeout(tick, 1000);
} else {

if(mins > 1) {
// countdown(mins-1);
setTimeout(function () { countdown(mins - 1); }, 1000);
}
}
}
tick();
}

countdown(3);
</script>

在这种情况下,顺序很重要。您需要确保文档在通过 DOM 访问该元素之前已经遇到该元素。

关于javascript - 出现错误 : Cannot set property 'innerHTML' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25968240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com