为什么设置了计时器数字不变化?

<script>

var num = document.getElementById("second");

var counter = num.innerHTML;

function count(){

counter--;

num.innerHTML=counter;

if(counter==0){

window.location.assign("http//:www.imooc.com");

}

}

setInterval("count()",1000);

function back(){

window.history.back();

}

</script>

<h1>操作成功</h1>

<span id="second">5</span>

<span>秒后回到主页</span>

<input type="button" value="返回" onclick="back()">


Auger_
浏览 1046回答 1
1回答

业余奶茶品鉴师

把js写到html后面就可以了,是渲染先后的问题,按照你的写法document.getElementById("decond")取不到按照下面那这样写就可以了,记得采纳<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <meta name="viewport" content="width=<device-width>, initial-scale=1.0">     <meta http-equiv="X-UA-Compatible" content="ie=edge">     <title>Document</title>     <style>            * {              padding: 0;              margin: 0;            }      </style> </head> <body>     <h1>操作成功</h1>     <span id="second">5</span>     <span>秒后回到主页</span>     <input type="button" value="返回" onclick="back()"> </body> <script>      var num = document.getElementById("second");      var counter = num.innerHTML;      function count() {            counter--;            num.innerHTML = counter;            if (counter == 0) {                  window.location.assign("http//:www.imooc.com");            }      }      setInterval("count()", 1000);      function back() {            window.history.back();      } </script> </html>
打开App,查看更多内容
随时随地看视频慕课网APP