history对象常用方法

window对象给我们提供了一个 history对象,与浏览器历史记录进行交互。该对象包含用户(在浏览器窗口中)访问过的URL。

history.back( )后退功能
history.forward( )前进功能
go(参数)

前进后退功能,如果是1,就前进一个页面;-1就后退一个页面

 示例:

创建3个页面,第二个页面来实现前进和后退功能

history1页面

<!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>history1.html</title>
    <style>
        *{
            padding:0px;
            margin:0px;
        }
        div{
            width:200px;
            height:100px;
            color:red;
            border:1px solid purple;
            margin-bottom: 50px;
        }
    </style>
</head>
<body>
    <div>我是history1.html页面</div>
    <a href="history2.html">跳转到history2.html页面</a>
</body>
</html>

history2页面 

<!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>history2.html</title>
</head>
<body>
    <a href="history3.html">history3.html</a>
    <input type="button" name="" id="" value="后退一步" onclick="goback()">
    <input type="button" name="" id="" value="前进一步" onclick="gofoward()">
 
    <script>
        //页面后退一步
        function goback(){
            //history.back();
            history.go(-1);
        }
        //页面前进一步
        function gofoward(){
            //history.foward();
            history.go(1);
        }
    </script>
</body>
</html>

history3页面 

<!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>history3.html</title>
</head>
<body>
    <h2>我是history3.html页面</h2>
    <script>
        for(var i=0;i<5;i++){
            for(var j=0;j<5-i;j++){
                document.write('♥');
            }
            document.write('<br>');
    }
    </script>
</body>
</html>
  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值