jquery插件实现搜索历史


Posted in jQuery onApril 24, 2021

每天一个jquery插件-做搜索历史,供大家参考,具体内容如下

效果如下

jquery插件实现搜索历史

代码部分

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title>做搜索历史</title>
  <script src="js/jquery-3.4.1.min.js"></script>
  <style>
   *{
    margin: 0px;
    padding: 0px;
   }
   #searchbox{
    /* border: 1px solid lightgray; */
    height: 40px;
    width: 720px;
    position: relative;
   }
   #searchinput{
    border: 1px solid lightgray;
    border-radius: 5px 0px 0px 5px;
    height: 28px;
    position: absolute;
    right: 45px;
    top: 5px;
    left: 5px;
    width: 670px;
    outline: none;
    text-indent: 12px;
    font-size: 12px;
    color: gray;
   }
   #searchinput:focus{
    border-color: rgb(252,25,68);
   }
   #searchinput:focus~#morebox{
    display:flex;
   }
   #searchbtn{
    height: 30px;
    width: 40px;
    border: none;
    border-radius: 0px 5px 5px 0px;
    background-color: rgb(252,25,68);
    position: absolute;
    right: 5px;
    top: 5px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
   }
   #searchbtn img{
    width: 25px;
    height: 25px;
   }
   #morebox{
    border: 1px solid lightgray;
    position: absolute;
    top: 40px;
    left: 5px;
    right: 5px;
    height: 370px;
    z-index: 7;
    border-radius: 2px;
    display: flex;
    display: none;
   }
   #push{
    flex: 1;
    /* border: 1px solid lightgray; */
    position: relative;
   }
   #history{
    /* display: none; */
    flex: 1;
    /* border: 1px solid lightgray; */
    position: relative;
   }
   .head{
    position: absolute;
    top: 0px;
    width: 100%;
    height: 30px;
    border-bottom: 1px solid lightgray;
    font-size: 12px;
    display: flex;
    align-items: center;
    text-indent: 12px;
    color: rgb(252,85,49);
   }
   .main{
    position: absolute;
    top: 30px;
    width: 100%;
    bottom: 0px;
    overflow-x:hidden;
    overflow-y: auto;
   }
   .item{
    font-size: 12px;
    height: 30px;
    display: flex;
    align-items: center;
    text-indent: 12px;
    cursor: pointer;
   }
   .item:hover{
    background-color: lightgray;
   }
  </style>
 </head>
 <body>
  <div id="searchbox">
   <input id="searchinput" placeholder="c一下" />
   <button id="searchbtn"><img src="img/sc.png"></button>
   <div id="morebox">
    <div id="history">
     <div class="head">搜索历史</div>
     <div class="main"></div>
    </div>
    <div id="push">
     <div class="head">热门推荐</div>
     <div class="main">
      <div class="item">微软终于对JDK下手了</div>
      <div class="item">小米隔空充电技术</div>
      <div class="item">Linux常用命令大全</div>
      <div class="item">阿飞超努力又水文了</div>
      <div class="item">每天学一个jquery插件竟然没有插件!究竟是道德的沦丧,还是人性的扭曲</div>
     </div>
    </div>
   </div>
  </div>
 </body>
</html>
<script>
 $(document).ready(function(){
  //每次点击搜索就假如缓存之中
  //
  $(".item").click(function(){
   var str = $(this).text();
   $("#searchinput").val(str)
  })
   // localStorage["history"] = '[]'//清除一下缓存;
  drawhistory();
  $("#searchbtn").click(function(){
   var str = $("#searchinput").val();
   if(str&&str!=""){
    var arr = getSession();
    arr.push(str);
    localStorage["history"] = JSON.stringify(arr);
    drawhistory();
   }
  })
  getSession();
  //根据缓存找到历史,然后生成搜索历史
  function drawhistory(){
   var arr = getSession();
   $("#history .main .item").remove();
   arr.forEach(item=>{
    var $item = $("<div class='item'>"+item+"</div>");
    $item.appendTo($("#history .main"));
   })
  }
  //获得缓存
  function getSession(){
   var ses = localStorage["history"];
   var arr = ses==undefined?[]:JSON.parse(ses);
   return arr;
  }
 })
</script>

思路解释

1、布局是个硬伤,我也不知道我这个布局是不是最合适的,不过看着没毛病
2、然后历史部分就是存到localStorage里面,在合适的动作的地方处理成对应的效果放回dom里面

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

jQuery 相关文章推荐
jQuery实现可拖动进度条实例代码
Jun 21 jQuery
jQuery UI Draggable + Sortable 结合使用(实例讲解)
Sep 07 jQuery
jQuery中ajax获取数据赋值给页面的实例
Dec 31 jQuery
webpack里使用jquery.mCustomScrollbar插件的方法
May 30 jQuery
解决jquery的ajax调取后端数据成功却渲染失败的问题
Aug 08 jQuery
JS与jQuery判断文本框还剩多少字符可以输入的方法
Sep 01 jQuery
在Vue项目中引入JQuery-ui插件的讲解
Jan 27 jQuery
jQuery表单元素过滤选择器用法实例分析
Feb 20 jQuery
jQuery位置选择器用法实例分析
Jun 28 jQuery
JQuery使用数组遍历跳出each循环
Sep 01 jQuery
JavaScript枚举选择jquery插件代码实例
Nov 17 jQuery
JS实现选项卡插件的两种写法(jQuery和class)
Dec 30 jQuery
jquery插件实现图片悬浮
jQuery实现影院选座订座效果
jQuery class属性操作addClass()与removeClass()、hasClass()、toggleClass()
jQuery treeview树形结构应用
Mar 24 #jQuery
jQuery实现鼠标拖动图片功能
Mar 04 #jQuery
ajax jquery实现页面某一个div的刷新效果
Mar 04 #jQuery
jquery实现广告上下滚动效果
Mar 04 #jQuery
You might like
图书管理程序(二)
2006/10/09 PHP
PHP中实现crontab代码分享
2015/03/26 PHP
php递归调用删除数组空值元素的方法
2015/04/28 PHP
关于PHP文件的自动运行方法分析
2016/05/13 PHP
thinkphp3.x中cookie方法的用法分析
2016/05/19 PHP
PHP串行化与反串行化实例分析
2016/12/27 PHP
PHP实现上传多图即时显示与即时删除的方法
2017/05/09 PHP
PHP实现验证码校验功能
2017/11/16 PHP
判断用户是否在线的代码
2011/03/05 Javascript
dtree 网页树状菜单及传递对象集合到js内,动态生成节点
2012/04/14 Javascript
JS简单的图片放大缩小的两种方法
2013/11/11 Javascript
利用js实现前台动态添加文本框,后台获取文本框内容(示例代码)
2013/11/25 Javascript
js/jQuery简单实现选项卡功能
2014/01/02 Javascript
jQuery实现Email邮箱地址自动补全功能代码
2015/11/03 Javascript
javascript简单判断输入内容是否合法的方法
2016/05/11 Javascript
jquery attr()设置和获取属性值实例教程
2016/09/25 Javascript
浅谈js继承的实现及公有、私有、静态方法的书写
2016/10/28 Javascript
jQuery+PHP实现上传裁剪图片
2020/06/29 jQuery
WebGL学习教程之Three.js学习笔记(第一篇)
2019/04/25 Javascript
es6函数之严格模式用法实例分析
2020/03/17 Javascript
Vue封装全局过滤器Filters的步骤
2020/09/16 Javascript
pyqt5 lineEdit设置密码隐藏,删除lineEdit已输入的内容等属性方法
2019/06/24 Python
python实现局域网内实时通信代码
2019/12/22 Python
Tensorflow读取并输出已保存模型的权重数值方式
2020/01/04 Python
Python进行统计建模
2020/08/10 Python
使用css3匹配手机屏幕横竖状态
2014/01/27 HTML / CSS
英国在线珠宝店:The Jewel Hut
2017/03/20 全球购物
台湾SHOPRO购物行家:亚洲首创影视.3C.家电.优质购物平台
2018/05/07 全球购物
护理工作感言
2014/01/16 职场文书
情侣吵架检讨书
2014/02/05 职场文书
医学专业应届生的自我评价
2014/02/28 职场文书
吃空饷专项整治方案
2014/10/27 职场文书
​(迎国庆)作文之我爱我的祖国
2019/09/19 职场文书
Python 批量下载阴阳师网站壁纸
2021/05/19 Python
教你如何用Python实现人脸识别(含源代码)
2021/06/23 Python
MySQL之MyISAM存储引擎的非聚簇索引详解
2022/03/03 MySQL