国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

基于JavaScript實現百度搜索框效果

2019-11-19 16:00:04
字體:
來源:轉載
供稿:網友

本文實例為大家分享了js實現百度搜索框展示效果的具體代碼,供大家參考,具體內容如下

具體代碼如下:

<!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、文本框獲取焦點,并且文本框中有內容的時候   2、在文本框中操作內容(新輸入/刪除),如果內容沒有清空,我們就顯示,否則就隱藏  */  //隱藏  /*   1、點擊頁面中其余的位置(除了點擊文本框和searchList里面的每一行)都隱藏;   2、點擊searchList中的列表隱藏,但是還要把列表中的內容放到文本框中  */  //不管是獲取焦點onfocus,還是在里面編輯內容onkeyup,都是有內容顯示,沒內容隱藏  var searchInp = document.getElementById('searchInp'),searchList = document.getElementById('searchList');  searchInp.onkeyup = searchInp.onfocus = function(){   var val = this.value.replace(/(^ +| +$)/g,'')//獲取文本框中的內容,并且去除它的首尾空格   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中的內容放在文本框中   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";  }  //我們可以阻止一個容器中某些特殊性的元素,讓其不在委托的范圍內:我們只需要把這些不需要委托的阻止冒泡傳播即可  searchInp.onclick = function(e){   e = e || window.event;   e.stopPropagation ? e.stopPropagation() : e.cancelBubble = "true";  } </script></body></html>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安丘市| 嵩明县| 永城市| 扎兰屯市| 左云县| 双鸭山市| 清镇市| 大理市| 丁青县| 武隆县| 阳谷县| 洛南县| 若尔盖县| 文成县| 九龙城区| 娱乐| 洪江市| 龙口市| 银川市| 诸暨市| 海兴县| 蒙城县| 扶风县| 萨迦县| 工布江达县| 临海市| 嘉善县| 如皋市| 德化县| 林口县| 吉安市| 潢川县| 刚察县| 通江县| 固始县| 美姑县| 隆昌县| 田林县| 固始县| 宝山区| 常德市|