JS--历史搜索记录的实现

公司最近要做商城,真是压力山大啊!还好招到了一个UI,要不然都不知道如何是好。

不罗嗦,商城一般都要有历史搜索记录的实现:

看一下效果图:


1.实现思路

1)用localStorage实现存储,如果重复的搜索内容不存就可以了

2)动态添加到列表中

2.html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8"/>
		<meta name="viewport" content="width=device-width"/>
		<title>搜索历史记录的使用</title>
		<link rel="stylesheet" href="./style.css"/>
		<script src="../jquery-3.1.0.min.js"></script>
	</head>

	<body>
		<div class="search-container">
			<div class="search-box">
				<input type="text" name="search" id="search"/>
			</div>

			<div class="search-btn">
				搜索
			</div>
		</div>

		<div class="search-history">
			<p>
				历史搜索:
			</p>

			<div class="clear-history">
			</div>
			<ul class="search-history-list">
			</ul>
		</div>
		<script src="./action.js"></script>
	</body>
</html>

2.css

body{
	margin:0;
	padding:0;
	font-family:"Helvetica","Microsoft Yahei";
}

ul,li,p{
	padding:0;
	margin:0;
}
input{
	border:none;
}


.search-container{
	position:relative;
	width:100%;
	overflow:hidden;
	height:40px;
	background-color:#d2d1d1;
}
.search-box{
	width:60%;
	height:30px;
	margin:5px auto;
	
}

.search-box input{
	width:100%;
	height:30px;
	border:1px solid #979ca0;
	border-radius:10px;
	outline:none;
}

.search-btn{
	position:absolute;
	height:40px;
	line-height:40px;
	right:6%;
	top:0;
	text-align:center;
	color:white;
}

/*历史记录*/

.search-history{
}

.search-history p{
	font-size:14px;
	height:30px;
	line-height:30px;
	width:70%;
	padding-left:5%;
	float:left;
	box-sizing:border-box;
}

.clear-history{
	width:30%;
	float:left;
	height:30px;
	background-image:url("./trash.png");
	background-repeat:no-repeat;
	background-size:15px 15px;
	background-position:center;
}

ul.search-history-list{
	padding:0 10px;
	clear:left;
	font-size:14px;
	color:grey;
}

ul.search-history-list li{
	list-style:none;
	width:100%;
	height:30px;
	line-height:30px;

	border-bottom:1px solid #eaeaea;
}



3.JS

详细的注释就不再啰嗦了。

function search(){
	$(".search-btn").click(function(){
		searchHistory($("#search").val());
		console.log(localStorage);
	});
}
search();

function searchHistory(search_value){
	var len=5;  //设定存储的历史记录个数
	if(search_value!=""&&
		!judgeRepeat(search_value))
	{
		insertToHistoryList(search_value,len);
		if(localStorage.length<len)//0 1 2 3 4
		{
			localStorage.setItem(localStorage.length,search_value);
		}else{
			for(var i=0;i<len;++i)
			{
				if(i==len-1)
				{
					localStorage.setItem(i,search_value);
					return;
				}
				var next_value=localStorage.getItem(i+1);
				localStorage.setItem(i,next_value);	
			}
		}

	}
}

/*如果搜索结果与本地存储相同,
  则不行存储
*/
function judgeRepeat(search_value){
	var repeat_bool=false;
	for(var key in localStorage)
	{
		if(search_value==localStorage.getItem(key))
		{
			return true;
		}
	}
}

/*将搜索结果插入到历史记录中*/
function insertToHistoryList(search_value,len)
{
	var str="<li>"+search_value+"</li>";
	if($(".search-history-list").children().length==0)
	{
		$(".search-history-list").append($(str));
	}else
	{
		if($(".search-history-list").children().length<len)
		{
			$(str).insertBefore($(".search-history-list li:eq(0)"));
		}else
		{
			$(".search-history-list li:last").remove();  //超过len个则移除最后一个
			$(str).insertBefore($(".search-history-list li:eq(0)"));
		}
	}
}


/*初始化历史记录列表*/
function buildHistory(){
	for(var i=0;i<localStorage.length;++i)
	{
		
		var str="<li>"+localStorage.getItem(localStorage.length-1-i)+
			"</li>";
		$(str).appendTo($(".search-history-list"));
	}
}

buildHistory();

/*清空历史搜索记录*/
$(".clear-history").click(function(){
	localStorage.clear();
	$(".search-history-list").empty();
	console.log("History has been cleared");
});



实现搜索历史记录和去重、缓存,可以使用uni-app提供的本地存储功能。 首先,需要定义一个对象用来保存搜索历史记录,例如: ```javascript const history = { list: [], // 保存搜索历史的数组 maxLen: 10, // 最大历史记录数 add(keyword) { // 添加历史记录的方法 const index = this.list.indexOf(keyword) if (index !== -1) { this.list.splice(index, 1) // 如果已存在该记录则先删除 } this.list.unshift(keyword) // 添加到数组头部 if (this.list.length > this.maxLen) { this.list.pop() // 如果超过最大长度则删除末尾记录 } }, clear() { // 清空历史记录的方法 this.list = [] } } ``` 然后,可以在搜索框的输入事件中调用 add 方法,将搜索关键字添加到历史记录中。同时,在页面加载时可以从本地存储中读取历史记录,例如: ```javascript onLoad() { const historyList = uni.getStorageSync('history') || [] // 从本地存储中读取历史记录 history.list = historyList // 覆盖 history 对象的 list 属性 } ``` 为了防止用户重复添加同一条历史记录,可以在 add 方法中先通过 indexOf 方法判断该记录是否已存在,如果存在则先删除原有记录。 ```javascript add(keyword) { // 添加历史记录的方法 const index = this.list.indexOf(keyword) if (index !== -1) { this.list.splice(index, 1) // 如果已存在该记录则先删除 } this.list.unshift(keyword) // 添加到数组头部 if (this.list.length > this.maxLen) { this.list.pop() // 如果超过最大长度则删除末尾记录 } } ``` 最后,为了实现历史记录的缓存功能,可以在 add 和 clear 方法中调用 uni.setStorageSync 方法将历史记录保存到本地存储中,例如: ```javascript add(keyword) { // 添加历史记录的方法 // ... uni.setStorageSync('history', this.list) // 将历史记录保存到本地存储中 }, clear() { // 清空历史记录的方法 this.list = [] uni.setStorageSync('history', this.list) // 将清空后的历史记录保存到本地存储中 } ``` 这样就可以实现搜索历史记录和去重、缓存的功能了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值