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

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

作者: Z丿Sir | 来源:发表于2019-08-07 15:04 被阅读0次

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

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

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>

相关文章

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

    工作中遇到的,记录下来方便日后查看! 这块需要考虑的还是很多的,需要考虑数据去重,数据排序,存储到本地不能超过多少...

  • 输入框点击事件

    输入框点击事件 搜索 输入框获取焦点;默认字消失;失去焦点显示默认字;如果非空失去焦点后就保留当前的字;不显示为默...

  • 6-3【微信小程序全栈开发课程】记录页面(三)--记录数据组件初

    1、功能介绍 在记录页面对记录数据进行展示,每条记录数据后面都有编辑按钮,点击编辑按钮,出现修改文本框,点击修改按...

  • 2019-05-07

    点击上方输入框搜索你喜欢的***

  • Taro微信小程序-地图

    前言 使用taro微信小程序+腾讯地图api,实现以下功能 输入框搜索地址下拉展示,点击地址选中 回到定位位置,位...

  • Android 折叠式流式布局

    最近有个需求,搜索的历史记录,超过两行需要折叠,后面有个展示展开的按钮,点击按钮展示4行数据。和京东的一样,只是京...

  • react-native-gifted-chat 实践指南

    messages(Array) - 消息数组,用于展示消息 有特定的格式 text(String) - 输入框的默...

  • vscode 搜索

    全局搜索 command + shift + f 展示/隐藏搜索输入框里的包含的文件和排序的文件 shift + ...

  • 0825-百度地图api开发-周国康

    作业 三级联动 搜索周边服务 鼠标点击获取地址和坐标 根据经纬度反响解析地址 出行方式选择 输入框提示 结果面板展示

  • 百度语音搜索

    语音搜索。先实现搜索界面。 定义输入框:search_page.xml 这个搜索框可以手动输入,也可以点击语音图标...

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/uebodctx.html