基于JavaScript實現(xiàn)百度搜索框效果
更新時間:2020年06月28日 10:54:42 作者:diasa
這篇文章主要為大家詳細介紹了基于JavaScript實現(xiàn)百度搜索框效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了js實現(xiàn)百度搜索框展示效果的具體代碼,供大家參考,具體內(nèi)容如下
具體代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> *{ margin:0; padding:0; font-size:14px; } input{ display:block; outline:none; } a{ display:block; text-decoration: none; color:#000; } a:hover,a:active,a:target{ text-decoration: none; color:#000; } ul,li{ list-style:none; } .box{ position:absolute; top:20px; left:50%; margin-left:-250px; width:500px; } .box input{ width:300px; height:35px; padding:0 10px; border:1px solid #008000; } .box ul{ display:none; position:relative; top:-1px; border:1px solid #008000; } .box ul li,.box ul li a{ height:35px; line-height:35px; } .box ul li a{ padding:0 10px; } .box ul li a:hover{ background:#ccc; } </style> </head> <body> <div class='box'> <input type="text" id='searchInp'> <ul id='searchList'> <li><a href="javascript:;">111111111111</a></li> <li><a href="javascript:;">2222222222</a></li> <li><a href="javascript:;">33333333333</a></li> <li><a href="javascript:;">444444444444</a></li> <li><a href="javascript:;">5555555555555</a></li> </ul> </div> <script> //顯示 /* 1、文本框獲取焦點,并且文本框中有內(nèi)容的時候 2、在文本框中操作內(nèi)容(新輸入/刪除),如果內(nèi)容沒有清空,我們就顯示,否則就隱藏 */ //隱藏 /* 1、點擊頁面中其余的位置(除了點擊文本框和searchList里面的每一行)都隱藏; 2、點擊searchList中的列表隱藏,但是還要把列表中的內(nèi)容放到文本框中 */ //不管是獲取焦點onfocus,還是在里面編輯內(nèi)容onkeyup,都是有內(nèi)容顯示,沒內(nèi)容隱藏 var searchInp = document.getElementById('searchInp'),searchList = document.getElementById('searchList'); searchInp.onkeyup = searchInp.onfocus = function(){ var val = this.value.replace(/(^ +| +$)/g,'')//獲取文本框中的內(nèi)容,并且去除它的首尾空格 searchList.style.display = val.length > 0 ? "block" : "none"; } document.body.onclick = function(e){ e = e || window.event; e.target = e.target || e.srcElement; //如果事件源是#searchList下的a標簽,我們讓searchList隱藏,并且把當前點擊這個a中的內(nèi)容放在文本框中 if(e.target.tagName.toLowerCase()==="a" && e.target.parentNode.parentNode.id==="searchList"){ searchList.style.display = "none"; searchInp.value = e.target.innerHTML; return; } //如果事件源是文本框還需要單獨的處理 // if(e.target.id === "searchInp"){ // return; // } searchList.style.display = "none"; } //我們可以阻止一個容器中某些特殊性的元素,讓其不在委托的范圍內(nèi):我們只需要把這些不需要委托的阻止冒泡傳播即可 searchInp.onclick = function(e){ e = e || window.event; e.stopPropagation ? e.stopPropagation() : e.cancelBubble = "true"; } </script> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript之a(chǎn)ppendChild、insertBefore和insertAfter使用說明
這幾天需要用到對HTML節(jié)點元素的刪/插操作,由于用到insertBefore方法的時候遇到了一些麻煩,現(xiàn)在作為知識的整理,分別對appendChild、insertBefore和insertAfter做個總結(jié)2010-12-12JS獲取本地文件并進行網(wǎng)絡(luò)傳輸?shù)拇a詳解
在web中如想要獲取用戶計算機上的文件我們通常會用到的方法是通過一個表單元素<input type="file">操作用戶選擇的文件,本文小編給大家介紹了JS獲取本地文件并進行網(wǎng)絡(luò)傳輸?shù)姆椒?需要的朋友可以參考下2024-08-08JavaScript實現(xiàn)自動跳轉(zhuǎn)文本功能
這篇文章主要為大家詳細介紹了JavaScript自動跳轉(zhuǎn)文本功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05mockjs,json-server一起搭建前端通用的數(shù)據(jù)模擬框架教程
下面小編就為大家分享一篇mockjs,json-server一起搭建前端通用的數(shù)據(jù)模擬框架教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12bootstrap-table實現(xiàn)表頭固定以及列固定的方法示例
這篇文章主要介紹了bootstrap-table實現(xiàn)表頭固定以及列固定的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-03-03