JavaScript-页面跳转1【基础】

这段代码展示了两个页面互相跳转的实现,每个页面都包含一个计时器在指定时间后自动跳转回原页面,同时提供了点击按钮立即跳转的功能。计时器使用了JavaScript的setInterval和clearInterval方法,页面结构和样式用HTML和CSS定义。

页面一

 <style>
     div {
         width: 500px;
         height: 200px;
         border: 1px solid red;
         margin: 30px auto;
         text-align: center;
     }
     p {
         font-size: 30px;
     }
 </style>
</head>
<body>
    <div>
        <p>5s后自动跳转页面二</p>
        <button>点击跳转</button>
    </div>
    
    <script>
        const p = document.querySelector("p")
        const btn = document.querySelector("button") 
        // 1
        let i = 5
        let m = setInterval(function(){
            i = --i
            p.innerHTML = `${i}s后自动跳转页面二`
            if(i === 0) {
                clearInterval(m)
                location.href = '4location对象.html'
            }
        },500)
        // 2
        // setTimeout(function(){
        //     location.href = '4location对象.html'
        // },5000)
        // 3
        btn.addEventListener('click',function(){
            location.href = '4location对象.html'
        })
    </script>
</body>

页面二

<style>
     div {
         width: 500px;
         height: 200px;
         border: 1px solid rgb(94, 255, 0);
         margin: 30px auto;
         text-align: center;
     }
     p {
         font-size: 30px;
     }
 </style>
</head>
<body>
    <div>
        <p>7s后自动跳转页面一</p>
        <button>点击跳转</button>
    </div>
    
    <script>
        const p = document.querySelector("p")
        const btn = document.querySelector("button") 
        // 1
        let i = 7
        let m = setInterval(function(){
            i = --i
            p.innerHTML = `${i}s后自动跳转页面一`
            if(i === 0) {
                clearInterval(m)
                location.href = '3location对象.html'
            }
        },700)
        // 2
        // setTimeout(function(){
        //     location.href = '3location对象.html'
        // },7000)
        // 3
        btn.addEventListener('click',function(){
            location.href = '3location对象.html'
        })
    </script>
</body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值