js历史记录
history.pushState({
num: 1
}, 'title-history', '?num=1');
history.pushState({
num:2
}, 'title-history', '?num=2');
history.pushState({
num: 3
}, 'title-history', '?num=3');
history.replaceState({
num: 4
}, 'title-history', '?num=4');
window.addEventListener('popstate', function (e) {
//打印当前页面的数据(状态信息)
console.log(e.state);
console.log(history.state)
}, false) // 当前页面地址为 http://localhost/index.html?num=4 以下代码均在浏览器控制台里面触发
// history.back() // 当前页面地址为 http://localhost/index.html?num=2
// history.forward() // 当前页面地址为 http://localhost/index.html?num=4'
// history.go(-2) // 当前页面地址为 http://localhost/index.html?num=1



代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.content {
width: 300px;
height: 300px;
border: 1px solid #000;
}
</style>
</head>
<body>
<input type="text" id="words">
<input type="button" value="搜索" id="btn">
<div class="content">
<ul id="showData">
</ul>
</div>
<script>
var words = document.getElementById('words');
var data = [{
name: 'HTML',
id: 'html'
}, {
name: 'CSS',
id: 'css'
}, {
name: 'JavaScript',
id: 'js'
}, {
name: 'es6',
id: 'es6'
}, {
name: 'es5',
id: 'es5',
}];
function renderDom(data) {
var str = '';
data.forEach(function (item, index) {
str += '<li>' + item.name + '</li>';
});
showData.innerHTML = str;
}
renderDom(data);
btn.onclick = function (e) {
var filterData = data.filter(function (item, index) {
return item.name.indexOf(words.value) > -1;
});
history.pushState({
word: words.value
}, null, '?word=' + words.value);
renderDom(filterData);
}
window.onpopstate = function (e) {
console.log(e);
var key = e.state ? e.state.word : '';
var filterData = data.filter(function (item, index) {
return item.name.indexOf(key) > -1;
});
renderDom(filterData);
words.value = key;
}
</script>
</body>
</html>
欢迎加入web前端冲击顶级高薪大厂学习群,群聊号码:820269529
js历史记录的更多相关文章
- 用HTML/JS/PHP方式实现页面延时跳转
WEB开发中经常会遇到页面跳转或延时跳转的需求,掌握各种页面跳转方式非常必要. 以下是我总结有用HTML/JS/PHP三类方式实现跳转的方法,例子皆为三秒后跳转到index.php 页面. 1,HTM ...
- js实现前端的搜索历史记录
最近在对接前台页面(WEB端)时,产品要求需记录下客户的搜索记录,我们是前后台完全分离的项目,根本不能保存的session域中,没办法,虽然作为后台开发,遇到需求就自己研究了一通,先看一下最终效果图, ...
- js之添加浏览器历史记录
如何生成一条历史记录 简单粗暴的方法,直接在当前页面的地址栏中输入地址 点击页面中有a标签的href 执行location.href = ‘xxx’(location.replace(‘xxx’)生成 ...
- fabric.js 翻转,复制粘贴,隐藏, 删除,历史记录,撤销, 剪切, 图层,组合打散,锁定等功能
用vue写的 显示,隐藏 hide(){ this.canvas.getActiveObject().set('opacity', 0).setCoords(); this.canvas.reques ...
- js点击历史记录
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 利用js实现 禁用浏览器后退| 去除上一个历史记录链接
也是查找了好多资料才找到的,这种方式,可以消除 后退的所有动作.包括 键盘.鼠标手势等产生的后退动作. <script language="javascript"> / ...
- js修改window对象中的url历史记录
//页面地址:http://localhost/11/account.html//访问页面后,地址变为:http://localhost/11/account.html?type=banana con ...
- 在文本框输入数据后,因为有历史记录,鼠标点或者敲回车这个历史记录时,请问会触发什么JS事件
非ie触发 oninput事件,ie触发>onpropertychange事件 jquery写法 $("#input").bind("input propertyc ...
- JS实现页面进入、返回定位到具体位置
最为一个刚入职不久的小白...慢慢磨练吧... JS实现页面返回定位到具体位置 其实浏览器也自带了返回的功能,也就是说,自带了返回定位的功能.正常的跳转,返回确实可以定位,但是有些特殊场景就不适用了. ...
随机推荐
- 核心API的使用(给定一个字符串,统计每个字符出现的次数)
/** * 给定一个字符串,统计每个字符出现的次数. 如:abdaewrwqask435a1aasd */public class ReplaceString { static int length; ...
- 手动抠下的wordpress登录页面样式
CSS文件 login.css body, html { height: 100%; margin: 0; padding: 0; } html[Attributes Style] { -webkit ...
- Linux 线程调度策略与线程优先级
Linux内核的三种调度策略 SCHED_OTHER 分时调度策略. 它是默认的线程分时调度策略,所有的线程的优先级别都是0,线程的调度是通过分时来完成的.简单地说,如果系统使用这种调度策略,程序将无 ...
- SQL Full Join 的 Where条件
SQL需求是损益视图串资产负债视图 用Excel透视表模拟出来的结果就是 用SQL查询如下: 当Where条件是左边的视图的时候 select 损益视图.年 ,损益视图.年月 ,损益视图.期间 ,损益 ...
- windows jenkins 卸载
如果下载的是war包,先在任务管理器上停止jenkins的服务,再删除jenkins整个文件
- Web服务器与浏览器的实现原理
Web服务器与浏览器的实现原理 第一部分 为什么会出现web程序? 单机程序不能共享功能的特性导致了客户机服务器模式的诞生 有一台服务器有特定功能的程序 其他计算机通过客户端程序与服务器交流间接使用 ...
- Python数据类型之数字
数字(数值) 整数 :123 (int型) 浮点数: 0.25(带小数点的数字即为浮点数,Float型) 布尔值:False,True(即0和1,bool型) 复数 (暂无资料,complex型) 整 ...
- ftpClient.retrieveFileStream导致FTPClient的后面操作失败
问题代码: FTPFile[] fs = ftpClient.listFiles(); for (FTPFile ff : fs) { if (ff.getName().equals(fileName ...
- servlet的url-pattern规则
https://www.cnblogs.com/canger/p/6084846.html
- MyBatis异常总结
1 Invalid bound statement 1 org.apache.ibatis.binding.BindingException: Invalid bound statement (not ...