输入框默认为空,点击文本框展示最近10条搜索记录

工作中遇到的,记录下来方便日后查看!

这块需要考虑的还是很多的,需要考虑数据去重,数据排序,存储到本地不能超过多少条,超过多少条需要删除等。

Style

 <style>
    .div{position:relative;}
    #note {width: 50%;overflow: hidden;text-align: left;height: 38px;border: 1px solid #ccc;border-radius: 10px;}
    #search {width: 141px;height: 37px;font-size: 14px;line-height: 14px;color: #959595;padding-bottom: 2px;}
    .hot-list {padding: 10px 0;width: 50%;position: absolute;left: 0px;top: 42px;margin: 0;line-height: 32px;font-size: 14px;background-color: #fff;display:one;border-radius: 4px;border: 1px solid #ccc;}
    .hot-list .itemList {text-indent: 15px;}
    .hot-list .itemList:hover {background: #e8eaed;}
 </style>

HTML

 <div class="div">
      <input name="note" id="note">
      <!--搜索框-->
      <button id="search">搜索</button>
      <!--历史记录-->
      <div class="hot-list"></div>
  </div>

JS

  <script>
         // 备注搜索框方法
        function noteMethod() {
            var hotList = $(".hot-list");
            var input = $("#note");
            $(".hot-list").on('mouseover', '.itemList', function () {
                $("#note").val($(this).html())
            });
            // 备注输入框点击
            input.click(function () {
                if (hisItem.length <= 0) {
                    return
                }
                if (!$(this).val()) {
                    setTimeout(function () {
                        hotList.show();
                    }, 100);
                }
            });
            // 备注输入框输入
            input.on("input", function () {
                if ($(this).val()) {
                    hotList.hide();
                }
            });
            // 点击隐藏
            $(document).click(function () {
                hotList.hide();
            });
        }
        noteMethod()

        /*
         *  备注存储数据处理
         */
        var hisTime; //获取时间数组
        var hisItem; //获取内容数组
        function noteValueInit() {
           hisTime = [];
           hisItem = [];
          // 数据去重
          for (var i = 0; i < localStorage.length; i++) {
              if (!isNaN(localStorage.key(i))) {
                  hisTime.push(localStorage.key(i))
              }
          }


          if (hisTime.length > 0) {
              hisTime.sort() // 排序
              for (var x = 0; x < hisTime.length; x++) {
                  localStorage.getItem(hisTime[x]).trim() && hisItem.push(localStorage.getItem(hisTime[x]));
              }
          }
          //十条数据,多余删除,同时删除localStorage 数据
          if (hisTime.length > 10) {
              let len_ = hisTime.length - 10
              for (let i = 0; i < len_; i++) {
                  localStorage.removeItem(hisTime[i]);
              }
              hisTime.splice(0, len_)
              hisItem.splice(0, len_)
          }
          // 执行noteValueInit(),每次清空之前添加的节点
          $(".hot-list").html('');
          // 将数据追加itemlist
          for (var i = 0; i < hisItem.length; i++) {
              $(".hot-list").prepend('<div class="itemList">' + hisItem[i] + '</div>');
          }
        }
        noteValueInit()
        // 点击搜索存储数据到localstroge
        $("#search").on('click', function () {
            var noteValue = $('#note').val();
            var time = (new Date()).getTime();
            // 将备注的信息放入localStorage做记录
            if ($.inArray(noteValue, hisItem) >= 0) {
                for (var j = 0; j < localStorage.length; j++) {
                    if (noteValue == localStorage.getItem(localStorage.key(j))) {
                        localStorage.removeItem(localStorage.key(j))
                    }
                }
                localStorage.setItem(time, noteValue)
            } else {
                localStorage.setItem(time, noteValue)
            }
            noteValueInit()
        })
        //苹果手机不兼容出现input无法取值以下是解决方法
        $(".delete").on("click", ".word-break", function () {
            var div = $(this).text();
            $('#sec').val(div);
        });
    </script>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,281评论 0 3
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,135评论 1 32
  • 一.简述如何安装配置apache 的一个开源的hadoop 1.使用root账户登陆 2.修改ip 3.修改hos...
    栀子花_ef39阅读 4,966评论 0 52
  • 天空不善言语 任由枝桠刺入身体 云朵羞赧 刚一走近又倏地跑远 你站在屋顶向我挥手 我假装看天 不看你 树叶窸窸窣窣...
    Rosebud668阅读 298评论 0 0
  • 在 v-for 块中,我们拥有对父作用域属性的完全访问权限。v-for 还支持一个可选的第二个参数为当前项的索引。...
    YJ_1101阅读 297评论 0 0