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实现页面返回定位到具体位置 其实浏览器也自带了返回的功能,也就是说,自带了返回定位的功能.正常的跳转,返回确实可以定位,但是有些特殊场景就不适用了. ...
随机推荐
- 线程池(Linux实现)
讨论QQ群:135202158 本文技术参考了sourceforge项目c thread pool,链接:http://sourceforge.net/projects/cthpool/ 线程池如上一 ...
- 十招谷歌 Google 搜索
十招谷歌搜索 一.或者 OR 二.网址 insite:example.com keyword 三.大约 1.类似查询(记得) ~keyword 2.模糊查询(记得) key*****word 3.模糊 ...
- iOS 之新特性界面
1.什么事新特性界面? 新特性界面就是第一次下载程序出现的界面,他的用途是帮助用户快速了解这款APP,所有说还是很有必要学一下的. 2.如何实现新特性界面? 实现思路:从本质上看,新特性界面就是一个全 ...
- 41.oracle索引,分析索引,索引碎片整理
概述 索引分为B树索引和位图索引.我们主要研究B树索引,B树索引如下图(图片源自网络):
- Android屏幕尺寸单位转换
最近在看Android群英传这本书,书中有一节涉及到了,屏幕尺寸与单位.觉得以后可能会用到,做个笔记. PPI(pixels per inch) ,又称为DPI,它是由对角线的像素点数除以屏幕的大小得 ...
- 对于maven创建spark项目的pom.xml配置文件(图文详解)
不多说,直接上干货! http://mvnrepository.com/ 这里,怎么创建,见 Spark编程环境搭建(基于Intellij IDEA的Ultimate版本)(包含Java和Scala版 ...
- interface类型
接口可使用的修饰符如下: InterfaceModifier: one of Annotation public protected private abstract static strictfp ...
- JavaScript数据结构-10.字典
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 机器学习 损失函数(Loss/Error Function)、代价函数(Cost Function)和目标函数(Objective function)
损失函数(Loss/Error Function): 计算单个训练集的误差,例如:欧氏距离,交叉熵,对比损失,合页损失 代价函数(Cost Function): 计算整个训练集所有损失之和的平均值 至 ...
- PHP之string之str_shuffle()函数使用
str_shuffle (PHP 4 >= 4.3.0, PHP 5, PHP 7) str_shuffle - Randomly shuffles a string str_shuffle - ...