版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/MMryyy/article/details/89477032

js实现搜索历史记录功能

首先我们来对比一下localStorage和sessionStorage:

localStorage生命周期是永久,这意味着除非用户显示在浏览器提供的UI上清除localStorage信息,否则这些信息将永远存在。

sessionStorage生命周期为当前窗口或标签页,一旦窗口或标签页被永久关闭了,那么所有通过sessionStorage存储的数据也就被清空了。

html

<div class="top"><div class="f1"><input type="text" class="input_top"/><span>取消</span></div></div><div class="history"><div class="history_top" style="display: none;"><span class="search">搜索历史</span><span class="del">删除历史</span></div><div class="contain"><div class="fr"></div></div></div>
<style type="text/css">.top{background:whitesmoke;line-height: 70px;padding-left: 10px;}.input_top{border-radius: 20px;width: 300px;height: 30px;margin-right: 50px;background:url(images/all_search.png) no-repeat left top ;background-size: 30px;padding-left: 30px;font-size: 18px;}:focus{outline: none;}.contain{margin: 10px;}.contain a{background: #E7E7E8;margin-bottom: 10px;margin-right: 10px;border-radius: 10px;padding: 5px 10px;line-height: 50px;}.info{height: 200px;width: 300px;background: url(images/pic12.png) no-repeat center;background-size: 300px 200px;padding: 50px;box-sizing: border-box;font-weight: 700;font-size: 20px;}.search,.del{background: beige;line-height: 30px;display: inline-block;border-radius: 5px;margin-top: 10px;padding: 0px 10px;}.search{margin-right: 180px;}
</style>

js

<script type="text/javascript">
//取值写入页面$(function(){//历史记录  localStorage.historyItemsvar str=localStorage.historyItems;var s = '';if(str== undefined){$(".contain").append('<div class="info"></div>');}else{var strs= [];strs=str.split("|");for(var i=0;i<strs.length;i++){s += "<a href='https://www.baidu.com/'  οnclick='dianji(this)'><p>"+strs[i]+"</p></a>";}$(".contain .fr").html(s);$('.history_top').show();}});//点击搜索历史function dianji(obj){var tem=objvar word=$(tem).text();setWorld(word);}//点击搜索$('.input_top').on('keydown',function(e){          if(e.keyCode=='13'){location.href='https://www.baidu.com/';var keyword=$('.input_top').val();setWorld(keyword);$('.input_top').val('');}})    //存值方法,新的值添加在首位//当为空的时候设置一个关键字进去,判断如果关键字是否存在,不存在就追加新的关键字  function setWorld(keyword){$('.history_top').show();
//      var {historyItems}=localStorage;var historyItems=localStorage.historyItemsif(historyItems===undefined){localStorage.historyItems=keyword;}else{historyItems=keyword+'|'+historyItems.split('|').filter(e=>e!=keyword).join('|');localStorage.historyItems=historyItems;            }       }//清除值$('.del').click(function(){localStorage.removeItem('historyItems');$('.contain .fr').html('');$('.history_top').hide();$('.contain').append('<div class="info"></div>');})$('.del').mouseover(function(){$(this).css('background','peachpuff')})$('.del').mouseout(function(){$(this).css('background','beige')})</script>

js实现搜索历史记录功能相关推荐

  1. JS实现搜索匹配功能

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. 微信小程序—实现搜索功能,搜索历史记录功能

    本文主要基于微信小程序实现和uni-app实现搜索商品和历史记录的功能. 不详细介绍,主看代码注释即可!! 1.搜索组件页面代码块 <template><view><!- ...

  3. 微信小程序显示用户搜索历史记录功能实现

    效果是点击首页输入框跳转到搜索页面,用户搜索后将搜索的内容在历史搜索中展示 如下图所示 首页输入框布局和样式这里我就不展示了 js就是点击跳转页面 历史搜索记录模块wxml <view clas ...

  4. 微信小程序 - 搜索历史记录功能(纯前端)

    前言 网上大部分教程提供的代码都非常臃肿且 BUG 多,本文以最健壮的代码实现该功能. 本示例可直接用于您的项目,利用缓存特性可永久存储(除非用户手动清除缓存), 完成了搜索后自动写入历史.排序最新在 ...

  5. uni-app - 搜索历史记录功能(纯前端)

    前言 网上大部分教程提供的代码都非常臃肿且 BUG 多,本文以最健壮的代码实现该功能,兼容多端. 本示例可直接用于您的项目,利用缓存特性可永久存储(除非用户手动清除缓存), 完成了搜索后自动写入历史. ...

  6. js百度搜索框功能模拟

    这里我们就废话不多说了,直接上代码 <!DOCTYPE html> <html><head><meta charset="UTF-8"&g ...

  7. HTML5 JQuery 实现搜索匹配功能

    HTML5 JQuery 实现搜索匹配功能 效果动态图: 源代码: all.html <!doctype html> <html> <head> <meta ...

  8. java搜索页面历史记录,使用JS location实现搜索框历史记录功能

    首先,来看下效果图(样式什么的就不必吐槽了哈) html代码 搜索记录: //用于保存记录信息 css代码 * { margin: 0; padding: 0; } input { border: 0 ...

  9. chosen.jquery.js 有搜索功能、多选功能的下拉框插件

    chosen.jquery.js 有搜索功能.多选功能的下拉框插件 官方源码:  https://github.com/harvesthq/chosen 该源码中的样例index.html有该插件的详 ...

  10. html table 筛选记录,JS实现table表格内针对某列内容进行即时搜索筛选功能

    JS实现table表格内针对某列内容进行即时搜索筛选功能 发布时间:2020-08-29 09:26:37 来源:脚本之家 阅读:103 作者:Marx-link 本文实例讲述了JS实现table表格 ...

最新文章

  1. java 数据聚合_Java数据聚合问题请教?
  2. android gradle tools 3.X 中依赖,implement、api 和compile区别
  3. python xpath语法-Python xpath表达式如何实现数据处理
  4. 浅析row_number()函数【HQL】
  5. 【tool】企业级开源分布式文件服务器搭建(FastDFS)
  6. vSphere共享存储全配置流程
  7. mysql 1021 disk full_大分区使用xfs文件系统存储备份遇到的问题
  8. Golang中MYSQL驱动
  9. shader之——shadowGun代码分析
  10. xp系统安装金蝶k3服务器配置,XP单机版安装金蝶K3的13.1版本号,金蝶K3Wise安装步骤,安装成功...
  11. Java中自定义比较器
  12. 普歌-码上鸿鹄团队:在Nuxt(vue)渲染模板中使用mock.js随机生成数据
  13. 项目经理怎么写周总结和周计划?
  14. tampermonkey如何寻找_Tampermonkey脚本安装问题及自用脚本推荐
  15. 【泡泡图灵智库】基于优化的视觉惯性SLAM与GNSS紧耦合
  16. 加强统筹布局和顶层设计,以技能、平台、应用为三大着力点推动人工智能突破发展...
  17. java中循环里使用throw new抛出异常问题
  18. 联想开机启动项按哪个_联想重装系统按哪个键|联想电脑重装系统按什么键
  19. 微软远程控制安卓汉化版
  20. 扬子晚报:杨百万 PK 巴菲特

热门文章

  1. 从原理上解释什么是DDR的ZQ校准?
  2. 【人人学-5G技术进阶】华为5G技术进阶测试答案(四)
  3. 3. 布莱叶盲文与二进制码
  4. Openstack API 开发 快速入门
  5. 电影级视频剪辑PR调色/FCP调色/达芬奇调色预设LUTs
  6. python逻辑回归模型建模步骤_从原理到代码,轻松深入逻辑回归模型!
  7. 软件工程-团队作业2
  8. Personalized Defect Prediction
  9. 我想开发一个小程序,大概需要多少钱?
  10. Manjaro第二天