搜索历史管理

历史记录效果图:

HTML代码:

 1 <div id="search">
 2   <div class="header">
 3     <div class="form">
 4       <span class="search"></span>
 5       <input type="text" class="input" placeholder="请输入查找的内容" autofocus maxlength="26" onkeyup="value=value.replace(/[^a-zA-0-9u4E00-u9FA5 ]/g,'')"/>
 6       <span class="cancel" style="display:none;"></span>
 7     </div>
 8     <div class="search-btn">search</div>
 9   </div>
10   <div class="content">
11     <div class="history-title">
12       <div>
13       </div>
14       <div>搜索历史</div>
15     </div>
16     <span v-for="item in history" class="history" v-on:click="historyBtn(item)">{{item}}</span>
17     <div class="history-box history-title" style="display: none;">清空历史记录</div>
18     <div class="hidden" style="display: none;">userId</div>
19 
20   </div>
21 
22 </div>

css样式:

 1 <style>
 2     .header {
 3       position: fixed;
 4       top: 0;
 5       left: 0;
 6       right: 0;
 7       height: 4.6667rem;
 8       background: #5ECDAF;
 9       z-index: 1000;
10       padding: 0 1.25rem;
11       display: flex;
12       text-align: center;
13       line-height: 4.6667rem;
14     }
15     .header .form {
16        100%;
17       margin: 10px 0;
18       height: 3rem;
19       background: #fff;
20       border-radius: 0.4167rem;
21       position: relative;
22     }
23     .search {
24       position: absolute;
25       left: 1.25rem;
26       top: 0.8333rem;
27        1.2143rem;
28       height: 1.2143rem;
29       background-size: 1.2143rem;
30     }
31     .form input {
32       display: block;
33       background: none;
34       border: none;
35       outline: none;
36       padding: 0.8333rem 3.5rem;
37       font-size: 1.25rem;
38       color: #7D7D7D;
39        66%;
40     }
41     .search-btn {
42        5.1667rem;
43       color: #FFFFFF;
44       font-size: 1.5rem;
45       text-align: right;
46     }
47     .cancel {
48       position: absolute;
49       right: 0px;
50       top: 0.6667rem;
51        2.6667rem;
52       height: 2.6667rem;
53       background-size: 2rem;
54     }
55     .content {
56       padding:5rem 0px 0 0px;
57       font-size:1.1667rem;
58     }
59     .history-title{
60       display:flex;
61       margin:0.8333rem 0 0 1.25rem;
62     }
63     .history-title img{
64       margin-right:0.8333rem;
65     }
66     .history {
67       display: inline-block;
68       font-size: 1rem;
69       margin: 0.8333rem 1.25rem;
70       background: #F8F8F8;
71       padding: 0px 1.6667rem;
72       color: #000000;
73       height: 2.5833rem;
74       line-height: 2.5833rem;
75       border-radius: 1.3333rem;
76       overflow: hidden;
77     }
78 
79   </style>

js代码:

  1 <script src="js/vue.js"></script>
  2 <script src="js/jquery.js"></script>
  3 <script>
  4   var Search=new Vue({
  5     el: '#search',
  6     data: {
  7       searHistory: '搜索历史',
  8       history: []
  9     },
 10     methods: {
 11       //点击搜索历史
 12       historyBtn: function(item){
 13         console.log(item);
 14         $('.input').val(item);
 15         // search();
 16       }
 17     }
 18   });
 19 
 20   $(function () {
 21     var inputValue = '';
 22     curValue = '';
 23     var userID = $('.hidden').text();//取得ID
 24     var getRecord = window.localStorage.getItem("record" + userID);//根据用户ID取到记录
 25     var recordArr1 = [];
 26     var recordArr2 = new Array(10);
 38 
 39     $('.input').on('keyup', function (event) {
 40       // search();
 41       if (event.keyCode == 13) {
 42         searchFun();
 43         addHistory();
 44       }
 45     })
 46 
 47     //点击删除input框的内容
 48     $('.cancel').on('click', function () {
 49       $('.input').val('');
 50       $('.cancel').hide();
 51       $('.content').show();
 52       $('.search-body').hide();
 53     })
 54 
 55 
 56     //点击搜索,同时将搜索记录下来
 57     $('.search-btn').on('click', function () {
 58       searchFun();
 59       addHistory();
 60     });
 61 
 62     //搜索请求方法
 63     var searchFun = function () {
 64       curValue = $('.input').val();
 65       console.log(curValue);
 66       //将输入的肩括号用转义字符代替
 67       curValue = curValue.replace(/</g, "&lt;").replace(/>/g, "&gt;");
 68       console.log(curValue);
 69       if (curValue) {
 70         $('.history').show();
 71         $('.history-box').show();
 72         //将数据存到本地数组中
 73         if (getRecord == null) {
 74           if (curValue) {
 75             var index = recordArr1.indexOf(curValue);
 76             if (index > -1) {
 77               recordArr1.splice(index, 1);
 78             }
 79             recordArr1.unshift(curValue);
 80             window.localStorage.setItem("record" + userID, recordArr1);
 81           }
 82         } else {
 83           if (curValue) {
 84             var index = recordArr2.indexOf(curValue);
 85             if (index > -1) {
 86               recordArr2.splice(index, 1);
 87             }
 88             recordArr2.unshift(curValue);
 89             window.localStorage.setItem("record" + userID, recordArr2);
 90           }
 91         }
 92         //发送请求获取搜索结果
 93         $.get('url', { key: curValue}, function (res) {
 94           //console.log(res);
 95           Search.doctors = res.doctors;
 96           if (res.doctors.length > 0) {
 97             $('.content').hide();
 98             $('.search-body').show();
 99           } else {
100             alert('搜索不到"' + curValue + '"');
101           }
102         })
103       }
104     };
105 
106     //添加到搜索历史中
107     var addHistory = function () {
108       getRecord = window.localStorage.getItem("record" + userID);
109       if (getRecord == null) {
110         return;
111       }
112       else {
113         $('.history-box').show();
114         recordArr2 = getRecord.split(',');
115         for (var i = 0; i < recordArr2.length; i++) {
116           if (recordArr2[i] == '' || recordArr2[i] == null || typeof (recordArr2[i]) == undefined) {
117             recordArr2.splice(i, 1);
118             i = i - 1;
119           }
120         }
121 
122       }
123 
124       if (recordArr2.length > 10) {
125         recordArr2.length = 10;
126       }
127       Search.history = recordArr2;
128     };
129     addHistory();
130 
131     //清空历史记录
132     $('.history-box').click(function () {
133       window.localStorage.removeItem("record" + userID);
134       window.localStorage.removeItem(recordArr1);
135       recordArr1 = [];
136       recordArr2 = [];
137       console.log(recordArr1);
138       $('.history').hide();
139       $('.history-box').hide();
140     })
141 
142   })
143 </script>
原文地址:https://www.cnblogs.com/bushan/p/11104340.html