历史记录html页面,HTML5 history 页面历史记录

DOM window 对象通过history对象提供了对浏览器历史的访问。

跳转属性

back(): 向后跳转

forward(): 向前跳转

go(): 跳转至第n页

// 在history中向后跳转

window.history.back();

window.history.go(-1);

// 向前跳转

window.history.forward();

window.history.go(1);

// 当前页

window.history.go(0);

添加和修改历史记录中的条目

pushState(状态对象,标题(可为空),URL(可选的)):记录历史记录

window.onpopstate:触发 调用历史记录

Document

*{

margin: 0;

padding: 0;

}

.body{

background: gray;

}

#box{

width: 800px;

height: 400px;

background-color: #ddd;

margin: 100px auto;

display: flex;

justify-content: space-around;

align-items: center;;

}

ul{

width: 200px;

height:300px;

border: 1px solid #fff;

border-bottom: none;

}

li{

list-style: none;

line-height: 100px;

border-bottom: 1px solid #fff;

text-align: center;

}

#con{

width: 500px;

height: 300px;

border: 1px solid#fff;

}

var list = document.getElementById('list');

var con = document.getElementById('con');

var json = {

'小米': '小米商城',

'华为':'华为商城',

'苹果': '苹果商城'

};

var str = '';

var arr = [];

for (var key in json) {

str +='

' + key + '';

arr.push(json[key])

}

list.innerHTML = str;

console.log('arr',arr);

//以上完成渲染数据

var lis = list.querySelectorAll('li');

for (var i = 0; i < arr.length; i++) {

lis[i].index = i;

console.log('lis',lis[i]);

lis[i].onclick = function () { // 实现点击切换

con.innerHTML = arr[this.index];

window.history.pushState(arr[this.index],'',this.dataset['name']);

}

}

window.onpopstate = function (e) {

console.log('11')

var e = e || e.target;

console.log(e)

con.innerHTML = e.state;

}

案例:tab切换,记录内容

304e76f52f08

image.png

304e76f52f08

image.png

304e76f52f08

image.png

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HTML本身并不提供历史记录页面的实现,但您可以在Web应用程序中使用JavaScript和浏览器提供的API来实现历史记录页面。 以下是一些基本的步骤: 1. 获取历史记录:使用JavaScript的window.history对象可以获取当前浏览器窗口中的历史记录。 2. 显示历史记录:使用HTML和JavaScript创建一个列表,将历史记录添加到列表中,并将其显示在页面上。 3. 清除历史记录:使用JavaScript的window.history对象中的方法可以清除浏览器中的历史记录。 这是一个简单的示例代码,可以帮助您开始实现历史记录页面: ```html <!DOCTYPE html> <html> <head> <title>History Page</title> <script> //获取历史记录并将其添加到列表中 function showHistory() { var historyList = document.getElementById("historyList"); var history = window.history; for (var i = history.length - 1; i >= 0; i--) { var li = document.createElement("li"); li.innerHTML = history[i]; historyList.appendChild(li); } } //清除历史记录 function clearHistory() { var history = window.history; history.go(-history.length); } </script> </head> <body onload="showHistory()"> <h1>History Page</h1> <button onclick="clearHistory()">Clear History</button> <ul id="historyList"></ul> </body> </html> ``` 这段代码将在页面加载时显示浏览器的历史记录,并在“Clear History”按钮上单击时清除所有历史记录。您可以根据自己的需要修改此代码,以适应您的Web应用程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值