search-history.js
1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
define(function( require, exports, module ) {
/*
引用包
*/
var _Domsearch = require( 'domsearch' );
// 数组去重
Array.prototype.unique = function() {
var res = [], hash = {};
for(var i=0, elem; (elem = this[i]) != null; i++) {
if (!hash[elem])
{
res.push(elem);
hash[elem] = true;
}
}
return res;
}
//html5本地存储
var storage = window.localStorage;
$( function(){
// 获取本地存储的数据
var storageData = [];
if( storage.searchStorage !== undefined ){
storageData = storage.searchStorage;
if( storageData.indexOf(',') > 0 ){
storageData = storage.searchStorage.split( ',' );
storageData = storageData.unique();
for(var i = 0; i < storageData.length; i++ ){
//key(i)获得相应的键,再用getItem()方法获得对应的值
$( '.search-list ul' ).append( '<li class="cell"><span class="title"><i class="m-icon icon-history"></i>'+storageData[i]+'</span></li>' );
}
}else{
storageData = new Array( storageData );
$( '.search-list ul' ).append( '<li class="cell"><span class="title"><i class="m-icon icon-history"></i>'+storageData+'</span></li>' );
}
}else{
$( '.clear-history .m-btn' ).css({'display':'none'});
}
//将搜索数据存到本地;
$( '#search-btn' ).click(function(){
var _val = $( '#search-val' ).val();
if( _val != '' ){
storageData.push( _val );
storageData = storageData.unique();
storage.searchStorage = storageData;
}
});
//清空本地数据
$( '.clear-history .m-btn' ).click(function(){
storage.removeItem( 'searchStorage' );
$( '.search-list ul li' ).remove();
$( this ).css({'display':'none'});
});
//点击选中数据
$( '.search-list ul' ).delegate( 'li' , 'click' ,function(){
$( '#search-val' ).val( $( this ).text() );
});
new _Domsearch( $( '#search-val' )[0] , {
dataItem: $( '.search-list ul li' ),
dataSource:'span'
});
});
});