實(shí)現(xiàn)步驟:1、使用setTimeout()函數(shù)指定一個(gè)定時(shí)器,并設(shè)置定時(shí)器中執(zhí)行跳轉(zhuǎn)函數(shù)的等待秒數(shù),語法“setTimeout(function(){//跳轉(zhuǎn)代碼},等待的毫秒數(shù));”;2、在跳轉(zhuǎn)函數(shù)中,設(shè)置“$(location).attr('href','頁面地址');”或“$(window).attr('location','頁面地址');”語句進(jìn)行頁面跳轉(zhuǎn)即可。
本教程操作環(huán)境:windows7系統(tǒng)、jquery3.6.0版本、Dell G3電腦。
jquery實(shí)現(xiàn)幾秒后跳轉(zhuǎn)頁面,可以分成兩個(gè)部分:
-
設(shè)置一個(gè)定時(shí)器,控制跳轉(zhuǎn)秒數(shù)
-
跳轉(zhuǎn)頁面
步驟1:使用setTimeout()函數(shù)指定一個(gè)定時(shí)器,并設(shè)置定時(shí)器中執(zhí)行跳轉(zhuǎn)函數(shù)的等待秒數(shù)
setTimeout()方法將在以毫秒為單位指定的時(shí)間后調(diào)用函數(shù)
語法格式可以是以下兩種:
setTimeout(要執(zhí)行的代碼, 等待的毫秒數(shù)) setTimeout(JavaScript 函數(shù), 等待的毫秒數(shù))
setTimeout() 是設(shè)定一個(gè)指定等候時(shí)間 (單位是千分之一秒, millisecond), 時(shí)間到了, 瀏覽器就會執(zhí)行一個(gè)指定的代碼,。
步驟2:在setTimeout()函數(shù)中,使用jQuery的屬性替換方法來實(shí)現(xiàn)跳轉(zhuǎn)
-
$(location).attr()
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="js/jquery-3.6.0.min.js"></script> <script> $(document).ready(function() { $("button").click(function() { setTimeout(function() { $(location).attr('href', 'http://www.php.cn'); }, 5000); }); }); </script> </head> <body> <button>實(shí)現(xiàn)5秒后跳轉(zhuǎn)頁面</button> </body> </html>
-
$(window).attr()
$(document).ready(function() { $("button").click(function() { setTimeout(function() { $(window).attr('location','http://www.php.cn'); }, 5000); }); });
【推薦學(xué)習(xí):jQuery視頻教程、web前端視頻】