为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Auger_ 2018-08-22 15:46:43
<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()">
查看完整描述

1 回答

?
业余奶茶品鉴师

TA贡献260条经验 获得超388个赞

把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>


查看完整回答
2 反对 回复 2018-08-22
  • 1 回答
  • 0 关注
  • 1045 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信