方法:1、給按鈕元素添加“onclick="location.href='url'"”語(yǔ)句進(jìn)行跳轉(zhuǎn)。2、首先給按鈕元素綁定點(diǎn)擊事件;然后指定觸發(fā)點(diǎn)擊事件時(shí),會(huì)執(zhí)行的函數(shù);最后在執(zhí)行函數(shù)中,使用“l(fā)ocation.href”語(yǔ)句設(shè)置跳轉(zhuǎn)功能。
本教程操作環(huán)境:windows7系統(tǒng)、javascript1.8.5版、Dell G3電腦。
方法1:在按鈕元素上直接使用location.href
進(jìn)行跳轉(zhuǎn)
<input type="submit" name="Submit" value="同意" onclick="location.href='http://www.php.cn'">
方法2:給按鈕元素綁定點(diǎn)擊事件,進(jìn)行跳轉(zhuǎn)
<!DOCTYPE html> <html> <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> </head> <body> <button>點(diǎn)點(diǎn)點(diǎn)</button> <div></div> <script> var btn = document.querySelector('button') var div = document.querySelector('div') btn.addEventListener('click', function () { // location.href = 'http://www.baidu.com'; var timer = 5; setInterval(function () { if (timer == 0) { location.href = 'http://www.php.cn'; } else { div.innerHTML = '你還有' + timer + '秒就可以跳轉(zhuǎn)了'; timer-- } },1000) /* setInterval(function(){ div.innerHTML = '你還有' + timer + '秒就可以跳轉(zhuǎn)了' timer--; },1000) */ }); </script> </body> </html>
【推薦學(xué)習(xí):javascript高級(jí)教程】