支付倒计时返回首页案例简介:在首页绑定一个按钮跳转到另一个页面,用到了简单的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>
<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" 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>
<script>
//逻辑:页面打开,开始倒计时
window.onload = function(){
//先赋值
let timer = 10;
//倒计时
//箭头函数()=>{} == function(){}
setInterval(()=>{
timer--;
document.getElementById('countDown').innerText = timer;
//等于0时跳转首页
if(timer==0){
location.href = "./index.html"
}
},1000);
}
//点击按钮立即返回首页
document.getElementsByTagName('button')[0].onclick = function(){
location.href = "./index.html"
}
</script>
</body>
</html>