javascript(jQuery)實現一個簡單的5秒倒計時,並跳轉網頁

原文鏈接:https://blog.csdn.net/lzpzwy/article/details/80072734
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>倒計時五秒</title>
		<script>
			//使用匿名函數方法
			function countDown(){
				
				var time = document.getElementById("Time");
				//alert(time.innerHTML);
				//獲取到id爲time標籤中的內容,現進行判斷
				if(time.innerHTML == 0){
					//等於0時清除計時
					window.location.href="https://www.baidu.com";
				}else{
					time.innerHTML = time.innerHTML-1;
				}
			}
			//1000毫秒調用一次
			setInterval(countDown,1000);
		</script>
		<style>
			#Time,#p{
				font-size: 100px;
				text-align: center;
			}
		</style>
	</head>
	<body>
		<p id="Time">5</p>
	</body>
</html>

用jQuery:

<!DOCTYPE html>
<html>
	<head>
        <meta charset="UTF-8">
        <title>倒計時五秒</title>
        <script src="js/jquery-1.12.4.min.js"></script>
        <script>
        //使用匿名函數方法
            function counttime() {
                var $time = $('#Time');
                if($time.html() === '1'){
                    window.location.href=("http://www.baidu.com");
                }else{
                    $time.html($time.html()-1);
                }
            }
            setInterval(counttime,1000);
        </script>
        <style>
            #Time,#p{
            font-size: 100px;
            text-align: center;
            }
        </style>
    </head>
    <body>
        <p id="Time">5</p>
    </body>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章