js實(shí)現(xiàn)百度淘寶搜索功能
本文實(shí)例為大家分享了js實(shí)現(xiàn)百度淘寶搜索功能的具體代碼,供大家參考,具體內(nèi)容如下
由于沒有后臺數(shù)據(jù),用數(shù)組模擬一下后臺返回的數(shù)據(jù)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> .box{ width:214px; /*height: 400px;*/ margin: 100px auto; position: relative; } </style> </head> <body> <div class="box"> <input type="text" id="txt"> <input type="button" id="btn" value="搜索"> </div> <script src="common.js"></script> <script> //模擬后臺的數(shù)據(jù) var keyWords = [ "一馬當(dāng)先", "一箭雙雕", "一絲不茍", "一心二用", "吃火鍋", "吃雞肉", "吃雞腿", "吃雞蛋", "哈1哈", "嘻1嘻", "嗚1嗚", ]; //給文本框注冊鍵盤松開事件 $query("#txt").onkeyup = function () { var txt = $query("#txt").value; if ($query(".box div")) { $query(".box").removeChild($query(".box div")); } if (txt.length == 0) { if ($query(".box div")) { $query(".box").removeChild($query(".box div")); } return; } var newArr = []; for (var i = 0; i < keyWords.length; i++) { if (keyWords[i].indexOf(txt) !=-1) { newArr.push(keyWords[i]); } } if (newArr.length > 0) { var newBox = document.createElement("div"); newBox.style.border = "1px solid red"; newBox.style.width = "100%"; $query(".box").appendChild(newBox); for (var j = 0; j < newArr.length; j++) { var newP = document.createElement("p"); newP.style.width = "100%"; newP.innerText = newArr[j]; newP.onmouseover = function () { this.style.backgroundColor = "yellow"; } newP.onmouseout = function () { this.style.backgroundColor = ""; } newBox.appendChild(newP); } } console.log(newArr);//打印匹配的數(shù)據(jù) } </script> </body> </html>
最后,附上效果圖,確實(shí)丑了點(diǎn)?
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JS仿百度搜索自動提示框匹配查詢功能
- js調(diào)用百度地圖及調(diào)用百度地圖的搜索功能
- JQuery+JS實(shí)現(xiàn)仿百度搜索結(jié)果中關(guān)鍵字變色效果
- JS 實(shí)現(xiàn)百度搜索功能
- JS實(shí)現(xiàn)仿google、百度搜索框輸入信息智能提示的實(shí)現(xiàn)方法
- JS+Ajax實(shí)現(xiàn)百度智能搜索框
- JS百度地圖搜索懸浮窗功能
- js實(shí)現(xiàn)百度搜索提示框
- JavaScript實(shí)現(xiàn)百度搜索框效果
- JS判斷來路是否是百度等搜索索引進(jìn)行彈窗或自動跳轉(zhuǎn)的實(shí)現(xiàn)代碼
相關(guān)文章
JS實(shí)現(xiàn)很實(shí)用的對聯(lián)廣告代碼(可自適應(yīng)高度)
這篇文章主要介紹了JS實(shí)現(xiàn)很實(shí)用的對聯(lián)廣告代碼,可實(shí)現(xiàn)固定相對位置懸浮展示及跟隨屏幕上下滑動等功能,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09不錯的用外部Javascript修正特定網(wǎng)頁內(nèi)容
不錯的用外部Javascript修正特定網(wǎng)頁內(nèi)容...2007-08-08如何用JS實(shí)現(xiàn)簡單的數(shù)據(jù)監(jiān)聽
這篇文章主要介紹了如何用JS實(shí)現(xiàn)簡單的數(shù)據(jù)監(jiān)聽,對數(shù)據(jù)監(jiān)聽感興趣的同學(xué),可以參考一下2021-05-05基于HTML5上使用iScroll實(shí)現(xiàn)下拉刷新,上拉加載更多
本文主要介紹在HTML5中使用iScroll實(shí)現(xiàn)下拉刷新,上拉加載更多數(shù)據(jù)的方法,主要就是寫了兩個自定義函數(shù)pullDownAction和pullUpAction,分別在下拉和上拉的事件中調(diào)用他們。2016-05-05JS中FileReader類實(shí)現(xiàn)文件上傳及時預(yù)覽功能
這篇文章主要為大家詳細(xì)介紹了JS中FileReader類實(shí)現(xiàn)文件上傳及時預(yù)覽功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-03-03