ASCII码 ASCII码

js实现支付倒计时返回首页

发布于:2022-03-04 11:42:39  栏目:技术文档

支付倒计时返回首页案例简介:在首页绑定一个按钮跳转到另一个页面,用到了简单的js语法,getElementsByTagName、location.href等。

index.html

效果图如下:`<!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"> <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>

  1. <script>
  2. //逻辑:点击支付按钮进行跳转页面
  3. //获得第二个(第一个是0)标签名为'button'的标签添加点击事件并且绑定一个函数
  4. document.getElementsByTagName('button')[1].onclick = function(){
  5. //跳转前的确认框
  6. let res = window.confirm('请确认支付');
  7. //判断是否为真,为真跳转
  8. if(res){
  9. //直接使用目录下的html页面,也可输入其他网站链接
  10. location.href = "./return.html"
  11. }
  12. }
  13. </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" content="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>

  1. <script>
  2. //逻辑:页面打开,开始倒计时
  3. window.onload = function(){
  4. //先赋值
  5. let timer = 10;
  6. //倒计时
  7. //箭头函数()=>{} == function(){}
  8. setInterval(()=>{
  9. timer--;
  10. document.getElementById('countDown').innerText = timer;
  11. //等于0时跳转首页
  12. if(timer==0){
  13. location.href = "./index.html"
  14. }
  15. },1000);
  16. }
  17. //点击按钮立即返回首页
  18. document.getElementsByTagName('button')[0].onclick = function(){
  19. location.href = "./index.html"
  20. }
  21. </script>

</body></html>`

相关推荐
阅读 +