将一段jquery,倒计时的代码做成一个function,然后页面运行。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js实现数字从1动态递增到10</title>
<script
  src="https://code.jquery.com/jquery-3.4.1.js"
  integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
  crossorigin="anonymous"></script>
</head>
<body>
<span id="span1">5</span> 
<script>
    countdown(5);  //调用函数;
        funtion countdown(getNum){ //函数获得所需要倒计时的描述;

            var num = getNum;
            var t = setInterval(function(){
               num--;
               var span1 = document.getElementById("span1");
               span1.innerText = num;
                    if(num==0){
                        clearInterval(t);
                    }       
            },1000);
        }

</script>
</body>
</html>

2个回答

方法定义的 function 拼写错误,代码运行报错语法错误,正确的拼写是:
funtion -> function.(文中少了一个字母 c)

countdown写在下面,function拼写要正确

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js实现数字从1动态递增到10</title>
<script
  src="https://code.jquery.com/jquery-3.4.1.js"
  integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
  crossorigin="anonymous"></script>
</head>
<body>
<span id="span1">5</span> 
<script>

        function countdown(getNum){ //函数获得所需要倒计时的描述;

            var num = getNum;
            var t = setInterval(function(){
               num--;
               var span1 = document.getElementById("span1");
               span1.innerText = num;
                    if(num==0){
                        clearInterval(t);
                    }       
            },1000);
        }
    countdown(5);  //调用函数;
</script>
</body>
</html>
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问