JavaScript實(shí)現(xiàn)前端實(shí)時(shí)搜索功能
大部分頁面都具備搜索功能。而作為前端,我們的目的只是將用戶輸入的內(nèi)容返回給后臺(tái)而后呈現(xiàn)反饋數(shù)據(jù)給用戶,具體實(shí)現(xiàn)如下:
1.基本布局:
<div class="searcher"> <p class="searcher-main"> <i><img src="img/icon/icon-search-map.png"/></i> <input class="searcher-text" placeholder="請(qǐng)輸入檔口名稱"></input> </p> <p class="searcher-cancel">取消</p> </div>
這里涉及一個(gè)問題如何將搜索圖標(biāo)放入input中,網(wǎng)上有相關(guān)資料不做贅述:
.searcher { background: rgba(255, 255, 255, 0); position: fixed; z-index: 999; width: 100%; height: 6rem; text-align: center; font-size: 1.6rem; } .searcher-main { background: #F4F4F4; position: absolute; left: 50%; top: 1.2rem; margin-left: -45%; border-radius: 1.6rem; width: 80%; height: 3rem; line-height: 3rem; } .searcher-text { width: 80%; text-align: center; border: none; outline: none; background: #F4F4F4; } .searcher-cancel { position: absolute; width: 10%; height: 3rem; line-height: 3rem; color: #929292; top: 1.2rem; right: 1rem; }
2.step-1:
3.js部分
這里要安利IE9以上的oninput事件
onchange事件只在鍵盤或者鼠標(biāo)操作改變對(duì)象屬性,且失去焦點(diǎn)時(shí)觸發(fā),腳本觸發(fā)無效。
onkeydown/onkeypress/onkeyup在處理復(fù)制、粘貼、拖拽、長(zhǎng)按鍵(按住鍵盤不放)等細(xì)節(jié)上并不完善。
onpropertychange不用考慮是否失去焦點(diǎn),不管js操作還是鍵盤鼠標(biāo)手動(dòng)操作,只要HTML元素屬性發(fā)生改變即可立即捕獲到。遺憾的是,onpropertychange為IE專屬的。
//監(jiān)聽input框,實(shí)時(shí)渲染 $('.searcher-text').on('input', function() { initSearchList(); });
JQ一般都是用這種+=html的方法,雖然累贅不過通過url或者tag標(biāo)簽里屬性傳參較容易理解。
//渲染搜索列表 function initSearchList() { var List = $('.searcher-land ul'); var params = {}; //搜索過濾字符 var SEARCH_KEY = $('.searcher-text').val() params['action'] = 'get_search_key_list'; params['market_iid'] = 1001; params['search_type'] = TYPE; params['search_key'] = replaceIllegalStr(SEARCH_KEY); epm.ajax(params, function(result) { console.log(result); console.log(TYPE) var html = ''; List.html(''); //有結(jié)果 if(result.data.length > 0) { $.each(result.data, function(index, value) { goodName = value['goods_name']; shopName = value['shop_name']; //判斷Name類型 itemName = (goodName) ? goodName : shopName; html += '<li class="goods-list">' + itemName + '</li>' }); $('.searcher-list').html(html); } //無結(jié)果 else { html = '<div class="no-goods">暫時(shí)無法找到此選項(xiàng)~</div>'; $('.searcher-list').html(html); } }); }
注意這里有一個(gè)replaceIllegalStr()方法,類似正則,目的是過濾掉一些無用的符號(hào)以免給后端接收數(shù)據(jù)帶來不必要的麻煩。
function replaceIllegalStr(str) { var reg; var illegal_list = ["/", "\\", "[", "]", "{", "}", "<", ">", "<", ">", "「", "」", ":", ";", "、", "•", "^", "'", "\"", "\r", "\r\n", "\\n", "\n"]; for (var i = 0; i < illegal_list.length; i++) { if (str.indexOf(illegal_list[i]) >= 0) { if (illegal_list[i] == '\\' || illegal_list[i] == '[') { reg = new RegExp('\\' + illegal_list[i], "g"); } else { reg = new RegExp(illegal_list[i], "g"); } str = str.replace(reg, ''); } } return str.trim(); }
4.step-2:
5.緩存
這里我們將點(diǎn)擊的數(shù)據(jù)保存在本地緩存里,供取用呈現(xiàn):
注: epm是自己封裝的一個(gè)方法與屬性的對(duì)象
//設(shè)置緩存 epm.setLocalItem = function(key, value) { if (window.localStorage) { localStorage.setItem(key, value); } else { //后備方案 setCookie(key, value); } };
//提取緩存 epm.getLocalItem = function(key) { if (window.localStorage) { return localStorage.getItem(key); } else { //后備方案 return getCookie(key); } };
//刪除緩存 epm.removeLocalItem = function(key) { if (window.localStorage) { localStorage.removeItem(key); } else { //后備方案 removeCookie(key); } };
6.step-3
得到點(diǎn)擊的相應(yīng)的緩存詞里的value,再次發(fā)送ajax:
更多搜索功能實(shí)現(xiàn)的精彩文章,請(qǐng)點(diǎn)擊專題:javascript搜索功能匯總 進(jìn)行學(xué)習(xí)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js實(shí)現(xiàn)帶搜索功能的下拉框?qū)崟r(shí)搜索實(shí)時(shí)匹配
- JS仿百度搜索自動(dòng)提示框匹配查詢功能
- js實(shí)現(xiàn)搜索框關(guān)鍵字智能匹配代碼
- JS使用replace()方法和正則表達(dá)式進(jìn)行字符串的搜索與替換實(shí)例
- JS 頁面內(nèi)容搜索,類似于 Ctrl+F功能的實(shí)現(xiàn)代碼
- javascript搜索自動(dòng)提示功能的實(shí)現(xiàn)
- JavaScript數(shù)組的快速克隆(slice()函數(shù))和數(shù)組的排序、亂序和搜索(sort()函數(shù))
- 用JS將搜索的關(guān)鍵字高亮顯示實(shí)現(xiàn)代碼
- js調(diào)用百度地圖及調(diào)用百度地圖的搜索功能
- JS實(shí)現(xiàn)表格數(shù)據(jù)各種搜索功能的方法
相關(guān)文章
JS使用oumousemove和oumouseout動(dòng)態(tài)改變圖片顯示的方法
這篇文章主要介紹了JS使用oumousemove和oumouseout動(dòng)態(tài)改變圖片顯示的方法,涉及javascript鼠標(biāo)事件及圖片操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03Bootstrap Paginator+PageHelper實(shí)現(xiàn)分頁效果
這篇文章主要為大家詳細(xì)介紹了Bootstrap Paginator+PageHelper實(shí)現(xiàn)分頁效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Javascript 圓角div的實(shí)現(xiàn)代碼
為什么要做圓角的div: 圓角div平滑美觀,某些情況下有比較不錯(cuò)的效果。比如說要做一個(gè)報(bào)message的消息框,那么動(dòng)態(tài)的生成一個(gè)圓角div則很有意義。而對(duì)html樣式控制的css本身是不直接支持圓角div的。2009-10-10???????基于el-table和el-pagination實(shí)現(xiàn)數(shù)據(jù)的分頁效果流程詳解
本文主要介紹了???????基于el-table和el-pagination實(shí)現(xiàn)數(shù)據(jù)的分頁效果,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-11-11JavaScript中 ES6 generator數(shù)據(jù)類型詳解
generator 是ES6引入的新的數(shù)據(jù)類型,由function* 定義, (注意*號(hào)),接下來通過本文給大家介紹js中 ES6 generator數(shù)據(jù)類型,非常不錯(cuò),感興趣的朋友一起學(xué)習(xí)吧2016-08-08