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实现页面返回定位到具体位置 其实浏览器也自带了返回的功能,也就是说,自带了返回定位的功能.正常的跳转,返回确实可以定位,但是有些特殊场景就不适用了. ...
随机推荐
- 基于Django+celery二次开发动态配置定时任务 ( 二)
一.需求 结合上一篇,使用djcelery模块开发定时任务时,定时任务的参数都保存在djcelery_periodictask表的args.kwargs字段里,并且是json格式.那么,当定时任务多了 ...
- week 4 Vocabulary in paper
1.Using action verbs 1.1 deffenence between action verbs and fuzzy verbs Action verbs(strong verbs) ...
- 递归实现快速幂(C++版)
快速幂是什么? 顾名思义,快速幂就是快速算底数的n次幂.其时间复杂度为 O(log₂N), 与朴素的O(N)相比效率有了极大的提高. 就以a的b次方来介绍: 把b转换成二进制数,该二进制数第i位的权为 ...
- Asp.net的生命周期之应用程序生命周期
参考:http://msdn.microsoft.com/zh-cn/library/ms178473(v=vs.100).aspx 参考:http://www.cnblogs.com/JimmyZh ...
- maven web不能创建src/main/java等文件等问题
我们在创建maven web项目的时候,默认只有src/main/resources这个source folder,我们按照maven结构添加src/main/java和src/test/java等s ...
- 【Python】解析Python的标准数据类型
目录结构: contents structure [-] 数值(Number) 数值类型 类型转化 Python中的Decimal数据类型 Python中的分数 Python中的算术方法 字符串(St ...
- python学习,day3:函数式编程,带参数
# coding=utf-8 # Author: RyAn Bi def test(x,y,z): print(x) print(y) print(z) test(y=2,z =3,x=1) #形参与 ...
- html5: 复制到剪贴板 clipboard.js
1.使用clipboard.min.js工具,引用此js 注意事项: IOS微信网页开发中,若使用此工具来开发复制功能,则需要在超链接/按钮上新增 onclick=" " 2.cl ...
- 关于 maven 打包直接运行的 fat jar (uber jar) 时需要包含本地文件系统第三方 jar 文件的问题
关于maven打包fat jar (uber jar) 时需要包含本地文件系统第三方jar文件的问题,今天折腾了一整天.最后还是用了spring boot来做.下面是几篇关于打包的有参考价值的文章,以 ...
- Git克隆与更新代码
一.克隆项目 除了可以向GitHub上提交项目外,更多的时候是我们到上面克隆(下载)优秀的开源项目来用,当然也可以将使用过程中发现的bug,通过建立分支的方式提交给项目的原作者. 现在的场景是在家将项 ...