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使用JSONP实现跨域获取数据的三种方法详解
May 04 jQuery
JQuery EasyUI 结合ztrIee的后台页面开发实例
Sep 01 jQuery
jQuery UI Draggable + Sortable 结合使用(实例讲解)
Sep 07 jQuery
Vue中正确使用jQuery的方法
Oct 30 jQuery
jQuery简单判断值是否存在于数组中的方法示例
Apr 17 jQuery
手写简单的jQuery雪花飘落效果实例
Apr 22 jQuery
jQuery+ajax实现动态添加表格tr td功能示例
Apr 23 jQuery
JS与jQuery实现ListBox上移,下移,左移,右移操作功能示例
May 31 jQuery
jQuery+vue.js实现的多选下拉列表功能示例
Jan 15 jQuery
jQuery 函数实例分析【函数声明、函数表达式、匿名函数等】
May 19 jQuery
如何解决jQuery 和其他JS库的冲突
Jun 22 jQuery
jQuery实现雪花飘落效果
Aug 02 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
PHP 设置MySQL连接字符集的方法
2011/01/02 PHP
PHP生成指定随机字符串的简单实现方法
2015/04/01 PHP
CodeIgniter配置之routes.php用法实例分析
2016/01/19 PHP
PHP排序算法之基数排序(Radix Sort)实例详解
2018/04/21 PHP
实例分析10个PHP常见安全问题
2019/07/09 PHP
jQuery使用手册之三 CSS操作
2007/03/24 Javascript
js 实现复制到粘贴板的功能代码
2010/05/13 Javascript
JavaScript代码复用模式实例分析
2012/12/02 Javascript
javascript创建动态表单的方法
2015/07/25 Javascript
JavaScript浏览器对象之一Window对象详解
2016/06/03 Javascript
EasyUI Datebox 日期验证之开始日期小于结束时间
2017/05/19 Javascript
AngularJS ng-repeat指令及Ajax的应用实例分析
2017/07/06 Javascript
深入理解Vue2.x的虚拟DOM diff原理
2017/09/27 Javascript
结合Vue控制字符和字节的显示个数的示例
2018/05/17 Javascript
jQuery实现的简单对话框拖动功能示例
2018/06/05 jQuery
微信小程序时间戳转日期的详解
2019/04/30 Javascript
简单了解JavaScript异步
2019/05/23 Javascript
sortable+element 实现表格行拖拽的方法示例
2019/06/07 Javascript
微信小程序文章详情功能完整实例
2020/06/03 Javascript
Ant design vue中的联动选择取消操作
2020/10/31 Javascript
Python re模块介绍
2014/11/30 Python
浅谈Python中的zip()与*zip()函数详解
2018/02/24 Python
python调用系统ffmpeg实现视频截图、http发送
2018/03/06 Python
Python实现多进程的四种方式
2019/02/22 Python
python2和python3实现在图片上加汉字的方法
2019/08/22 Python
Python 闭包,函数分隔作用域,nonlocal声明非局部变量操作示例
2019/10/14 Python
基于Python实现人脸自动戴口罩系统
2020/02/06 Python
解决Jupyter notebook中.py与.ipynb文件的import问题
2020/04/21 Python
Python中常用的os操作汇总
2020/11/05 Python
pycharm 快速解决python代码冲突的问题
2021/01/15 Python
Myprotein俄罗斯官网:欧洲第一运动营养品牌
2019/05/05 全球购物
JoJo Maman Bébé爱尔兰官网:英国最受欢迎的精品母婴品牌
2020/12/20 全球购物
营业员个人总结的自我评价
2013/10/25 职场文书
党的群众路线教育实践活动心得体会(教师)
2014/10/31 职场文书
优秀员工推荐材料
2014/12/20 职场文书
2015年班长个人工作总结
2015/04/03 职场文书