基于JavaScript實(shí)現(xiàn)百度搜索框效果
本文實(shí)例為大家分享了js實(shí)現(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、文本框獲取焦點(diǎn),并且文本框中有內(nèi)容的時(shí)候
2、在文本框中操作內(nèi)容(新輸入/刪除),如果內(nèi)容沒有清空,我們就顯示,否則就隱藏
*/
//隱藏
/*
1、點(diǎn)擊頁面中其余的位置(除了點(diǎn)擊文本框和searchList里面的每一行)都隱藏;
2、點(diǎn)擊searchList中的列表隱藏,但是還要把列表中的內(nèi)容放到文本框中
*/
//不管是獲取焦點(diǎn)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標(biāo)簽,我們讓searchList隱藏,并且把當(dāng)前點(diǎn)擊這個(gè)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;
}
//如果事件源是文本框還需要單獨(dú)的處理
// if(e.target.id === "searchInp"){
// return;
// }
searchList.style.display = "none";
}
//我們可以阻止一個(gè)容器中某些特殊性的元素,讓其不在委托的范圍內(nèi):我們只需要把這些不需要委托的阻止冒泡傳播即可
searchInp.onclick = function(e){
e = e || window.event;
e.stopPropagation ? e.stopPropagation() : e.cancelBubble = "true";
}
</script>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript之a(chǎn)ppendChild、insertBefore和insertAfter使用說明
這幾天需要用到對(duì)HTML節(jié)點(diǎn)元素的刪/插操作,由于用到insertBefore方法的時(shí)候遇到了一些麻煩,現(xiàn)在作為知識(shí)的整理,分別對(duì)appendChild、insertBefore和insertAfter做個(gè)總結(jié)2010-12-12
JS獲取本地文件并進(jìn)行網(wǎng)絡(luò)傳輸?shù)拇a詳解
在web中如想要獲取用戶計(jì)算機(jī)上的文件我們通常會(huì)用到的方法是通過一個(gè)表單元素<input type="file">操作用戶選擇的文件,本文小編給大家介紹了JS獲取本地文件并進(jìn)行網(wǎng)絡(luò)傳輸?shù)姆椒?需要的朋友可以參考下2024-08-08
JavaScript實(shí)現(xiàn)自動(dòng)跳轉(zhuǎn)文本功能
這篇文章主要為大家詳細(xì)介紹了JavaScript自動(dòng)跳轉(zhuǎn)文本功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
mockjs,json-server一起搭建前端通用的數(shù)據(jù)模擬框架教程
下面小編就為大家分享一篇mockjs,json-server一起搭建前端通用的數(shù)據(jù)模擬框架教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
微信小程序?qū)崿F(xiàn)animation動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)animation動(dòng)畫的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
bootstrap-table實(shí)現(xiàn)表頭固定以及列固定的方法示例
這篇文章主要介紹了bootstrap-table實(shí)現(xiàn)表頭固定以及列固定的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
微信小程序?qū)崿F(xiàn)電影App導(dǎo)航和輪播
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)電影App導(dǎo)航和輪播,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11

