console
let search = document.getElementById("searchInput");
search.onfocus = () => {
document.getElementsByClassName("maskLayer")[0].style.display = "block";
var searchArr;
if (localStorage.search) {
searchArr = localStorage.search.split(",");
console.log(searchArr)
for(let i =0;i<searchArr.length;i++){
let li = document.createElement('li');
li.innerText = searchArr[i];
document.getElementsByClassName('select-list')[0].appendChild(li);
}
} else {
searchArr = [];
}
}
search.onblur = function () {
document.getElementsByClassName("maskLayer")[0].style.display = "none";
};
function searchFor(data) {
let search_val = document.getElementById("searchInput").value;
let searchArr;
if(localStorage.search){
searchArr = localStorage.search.split(",");
}else{
searchArr =[];
}
searchArr.push(search_val)
localStorage.search = searchArr;
document.getElementsByClassName('select-list')[0].innerHTML='';
}
function clearHistory(data) {
console.log('清除历史记录:'+localStorage.search);
document.getElementsByClassName('select-list')[0].innerHTML='';
localStorage.removeItem('search');
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试页面</title>
<link type="text/css" rel="stylesheet" href="styles/main.css">
</head>
<body>
<div class="app-container">
<div class="maskLayer"></div>
<div class="search-div">
<input id="searchInput" type="text" placeholder="搜索您想要查询的项目"/>
<div class="search-drop-history">
<div class="select-list">
</div>
<div class="search-btn" onclick="searchFor(this)">搜索</div>
<div class="clear-btn" onclick="clearHistory(this)">清除历史记录</div>
</div>
</div>
</div>
<script src="scripts/main.js"></script>
<script>
</script>
</body>
</html>
body {
background-color: wheat;
margin: 0;
padding: 0;
}
.app-container {
width: 100vw;
height: 100vh;
background-color: chocolate;
}
.search-div {
width: 600px;
position: absolute;
z-index: 10000;
left: 50%;
top: 32%;
transform: translate(-50%, 0);
}
#searchInput {
width: 100%;
height: 38px;
font-size: 24px;
border-radius: 10px;
box-sizing: border-box;
}
.search-drop-history {
border-radius: 10px;
background-color: white;
margin: 4px;
}
.maskLayer {
background-color: #000;
width: 100%;
height: 100%;
z-index: 2;
position: absolute;
text-align: center;
display: none;
opacity: 0.7;
}
.clear-btn{
width: 102px;
height: 30px;
cursor: pointer;
right: 0;
background-color: #0f9d58;
border: 2px solid lightslategray;
position: absolute;
border-radius: 14px;
}
.search-btn{
width: 40px;
height: 30px;
background-color: antiquewhite;
border-radius: 10px;
text-align: center;
position: absolute;
cursor: pointer;
}
.select-list{
margin: 12px;
}