1. 程式人生 > 程式設計 >js實現支付倒計時返回首頁

js實現支付倒計時返回首頁

支付倒計時返回首頁案例簡介:在首頁繫結一個按鈕跳轉到另一個頁面,用到了簡單的語法,getElementsByTagName、location.href等。

index.html

效果圖如下:

js實現支付倒計時返回首頁

js實現支付倒計時返回首頁

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">http://www.cppcns.com
<title>Document</title> <style> .wrapper{ background-color:aquamarine; width: 300px; height: 300px; margin: 0 auto; } h2{ text-align: center; } button{ text-align: center; margin-left: 68px; } </style> </head> <body> <div class="wrapper"> <h2>商品:smile</h2> <h2>價格:infinity</h2> <h2>支付方式:Net</h2> <h2>訂單號:123456789</h2> <button>取消</button> <button>支付</button> </div> <script> //邏輯:點選支付按鈕進行跳轉頁面 //獲得第二個(第一個是0)標籤名為'button'的標籤新增點選事件並且繫結一個函式 document.getElementsByTagName('button')[1].onclick = function(){ //跳轉前的確認框 let res = window.confirm('請確認支付'); //判斷是否為真,為真跳轉 if(res){ //直接使用目錄下的html頁面,也可輸入其他連結 location.href = "./return.html" } } </script> </body> </html>

return.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" conten客棧t="width=device-width,initial-scale=1.0">
    <title>Document</title>
    <style>
        .wrapper{
            width: 300px;
            height: 400px;
            margin: 0 auto;
        }
        .content{
          
text-align: center; } </style> </head> <body> <div class="wrapper"> <div class="content"> <h2>支付成功</h2> <span id="countDown">10</span>秒後返回首頁 <button>立即返回</button> </div> </div> <script> //邏輯:頁面開啟,開始倒計時 window.onload = function(){ //先賦值 let timer = 10; //倒計時 //箭頭函式()=>{} == function(){} setInterval(()=>{ timer--; document.getElementById('countDown').inn程式設計客棧erText = timer; //等於0時跳轉首頁 if(timer==0){ location.href = "./index.html" } },1000); } //點選按鈕立即返回首頁 document.getElementsByTagName('button')[0].onclick = function(){ location.href = "./index.html" } </script> </body> </html>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。