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

这段代码哪里错了为什么超链接点击没效果而且减到0后也没有跳转页面

<!DOCTYPE html>

<html>

 <head>

  <title>浏览器对象</title>  

  <meta http-equiv="Content-Type" content="text/html; charset=gkb"/>   

 </head>

 <body>

  <!--先编写好网页布局-->

  <h1>操作成功</h1>

  <p><span id="second">5</span>秒后回到主页<a href="javascipt:back();"style="color:green">返回</a></p>

 

  <script type="text/javascript">  

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

   //获取显示秒数的元素,通过定时器来更改秒数。

    function time(){

        num--;

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

        }

    //通过window的location和history对象来控制网页的跳转。

    if(num==0)

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

    setInterval("time()",1000); 

   function back(){

       window.history.go(-1);

   }

 </script> 

</body>

</html>


正在回答

4 回答

1,首先你的if语句的格式就有问题,if(){}你少了“{}”。

2,window.location.assign()应该改成window.location.href="";

3,整个if语句应该是在函数function time(){}里面的。

<script type="text/javascript">
   
 var num=document.getElementById("second").innerHTML;
   function time(){
       document.getElementById("second").innerHTML=num;
       num--;
       if(num==0){
           window.location.href= "http://www.imooc.com";
       }
   }
   setInterval(time, 1000);
 
   function back(){
       window.history.go(-1);
   }

</script>

0 回复 有任何疑惑可以回复我~
#1

linda2017 提问者

非常感谢!
2016-09-14 回复 有任何疑惑可以回复我~
#2

linda2017 提问者

if语句下只有一行的时候可以不加{}的,有区别吗
2016-09-14 回复 有任何疑惑可以回复我~

多看同学代码,自己琢磨,收获多多

0 回复 有任何疑惑可以回复我~

每执行一次time()都要判断一下时间

0 回复 有任何疑惑可以回复我~

function time(){

        num--;

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

        

    //通过window的location和history对象来控制网页的跳转。

    if(num <=0)

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

    setInterval("time()",1000);

0 回复 有任何疑惑可以回复我~
#1

linda2017 提问者

还是没变化
2016-08-24 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

这段代码哪里错了为什么超链接点击没效果而且减到0后也没有跳转页面

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信