使用JS location實(shí)現(xiàn)搜索框歷史記錄功能
首先,來看下效果圖(樣式什么的就不必吐槽了哈)
html代碼
<form id="hisform"> <div id="searchbox"> <input id="inpt" type="text" autocomplete="off" /> <input id="btn" type="button" value="搜索" /> <div id="historybox"> <h3>搜索記錄:</h3> //用于保存記錄信息 <ul id="list"> </ul> </div> </div> </form>
css代碼
* { margin: 0; padding: 0; } input { border: 0; vertical-align: middle; float: left; } #searchbox { width: 300px; height: 50px; background: #fff; margin: 100px auto; border: 1px solid #ccc; position: relative; } #inpt { width: 240px; height: 50px; outline: none; text-indent: 10px; } #btn { width: 60px; height: 50px; cursor: pointer; } /* 歷史記錄框 */ #historybox { width: 280px; padding: 10px 10px 50px; border: 1px solid #ccc; position: absolute; top: 50px; left: -1px; /* 隱藏 */ display: none; } #historybox h3 { margin-bottom: 10px; } #list { list-style: none; } #list .on { float: left; border: 1px solid #ccc; background: #aaa; height: 30px; line-height: 30px; margin: 0 2px; border: 1px solid #ccc; border-radius: 5px; } #list .active { color: #fff; text-decoration: none; padding: 2px; }
js代碼(這里需引入jQuery)
$(function () { let max_history = 7;// 存儲(chǔ)最大歷史數(shù)據(jù) // 鼠標(biāo)移入事件 $('#inpt').on('focus', function () { $('inpt').val = ''; let data = localStorage.getItem('data'); //從本地存儲(chǔ)中讀取數(shù)據(jù) if (!data) { $('#historybox').css('display', 'none'); } else { $('#historybox').css('display', 'block'); historydata(JSON.parse(data)); // 渲染數(shù)據(jù) } }) // 鼠標(biāo)移出事件 $('#inpt').on('blur', function () { $('#historybox').css('display', 'none'); init_history();// 初始化歷史記錄,清空記錄 }) //點(diǎn)擊搜索按鈕時(shí),將搜索內(nèi)容添加到本地存儲(chǔ) $('#btn').on('click', function () { var search = inpt.value; var data = localStorage.getItem('data'); //從本地存儲(chǔ)中讀取數(shù)據(jù) if (data) { var arr = JSON.parse(data); //如果有數(shù)據(jù)則轉(zhuǎn)換成對(duì)象或數(shù)組 } else { var arr = []; //如果沒有數(shù)據(jù),則新增一條 } arr.push(search); removalDuplicate(arr);// 對(duì)用戶輸入值進(jìn)行處理(去重-篩選) localStorage.setItem('data', JSON.stringify(arr)); //將數(shù)據(jù)寫入到本地存儲(chǔ)中 }) // 數(shù)組去重-篩選函數(shù) function removalDuplicate(arr) { for (let i = 0; i < arr.length; i++) { var arritem = arr[i].trim(); // 去除字符串兩端空格 // 如果值為空,則不添加 if (arritem == '') { arr.splice(i, 1); } if (arritem !== "") { for (let j = i + 1; j < arr.length; j++) { if (arr[i] == arr[j]) { arr.splice(i, 1);//如果第二次輸入的值與第一次相同,則添加第二次的值 } } } } return arr; } // 渲染數(shù)據(jù) function historydata(searchArr) { searchArr.reverse();//反轉(zhuǎn),從后往前添加 // 遍歷出數(shù)據(jù) if (searchArr.length <= max_history) {//如果存儲(chǔ)數(shù)據(jù)小于等于max_history,則遍歷渲染 for (let i = 0; i < searchArr.length; i++) { $('#list').append(`<li class='on'><a href='#' class='active'>${searchArr[i]}</a><li>`); } } else {//否則渲染最大歷史記錄條數(shù) for (let i = 0; i < max_history; i++) { $('#list').append(`<li class='on'><a href='#' class='active'>${searchArr[i]}</a><li>`); } } } // 初始化-清空歷史記錄 function init_history() { $('#list').html(''); } })
總結(jié)
以上所述是小編給大家介紹的使用JS location實(shí)現(xiàn)搜索框歷史記錄功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
JavaScript原生節(jié)點(diǎn)操作小結(jié)
本文主要介紹了JavaScript原生節(jié)點(diǎn)操作的相關(guān)知識(shí)。具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01返回頁面頂部top按鈕通過錨點(diǎn)實(shí)現(xiàn)(自寫)
用戶在使用系統(tǒng)時(shí),會(huì)有很多表單的操作然而很多表單就會(huì)很長,所以就需要一個(gè)返回頁面頂部的top按鈕啦,于是自己寫了一個(gè),喜歡的朋友可以參考下2013-08-08一文詳解GoJs中g(shù)o.Panel的itemArray屬性
這篇文章主要為大家介紹了一文詳解GoJs中g(shù)o.Panel的itemArray屬性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05JS獲取中文拼音首字母并通過拼音首字母快速查找頁面內(nèi)對(duì)應(yīng)中文內(nèi)容的方法【附demo源碼】
這篇文章主要介紹了JS獲取中文拼音首字母并通過拼音首字母快速查找頁面內(nèi)對(duì)應(yīng)中文內(nèi)容的方法,涉及javascript針對(duì)字符串的遍歷、查找、正則匹配及轉(zhuǎn)換等操作技巧,并附帶完整demo源碼供讀者下載參考,需要的朋友可以參考下2016-08-08webpack HappyPack實(shí)戰(zhàn)詳解
這篇文章主要介紹了webpack HappyPack實(shí)戰(zhàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10你有必要知道的10個(gè)JavaScript難點(diǎn)
10個(gè)JavaScript難點(diǎn),你可能還不知道,不著急,本文為大家一一列出,一一攻破,感興趣的小伙伴們可以參考一下2017-07-07關(guān)閉瀏覽器時(shí)提示onbeforeunload事件
這篇文章主要介紹了關(guān)閉瀏覽器時(shí)提示onbeforeunload事件,有需要的朋友可以參考一下2013-12-12微信小程序 wx:for遍歷循環(huán)使用實(shí)例解析
這篇文章主要介紹了微信小程序 wx:for遍歷循環(huán)使用實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09