[toc]
```
<!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>
*{
margin:0;
padding:0;
}
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;
}
html,body{
width:100%;
height:100%;
border-top:1px solid transparent;
}
.box{
margin:20px auto;
width:500px;
}
.box input{
padding:0 10px;
width:300px;
height:35px;
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="searchInput">
<ul id="searchList">
<li><a href="javascript:;">ahhh-front</a></li>
<li><a href="javscript:;">ahhh-nodejs</a></li>
<li><a href="javscript:;">ahhh-html5</a></li>
<li><a href="javscript:;">ahhh-css3</a></li>
<li><a href="javscript:;">ahhh-who says</a></li>
</ul>
</div>
<script>
//什么時候顯示:
//1)文本框獲取焦點,并且文本框中有內容的時候
///2)在文本框操作內容的的時候(新輸入/刪除),如果內容沒有清空,我們就顯示,否則就隱藏
//->隱藏
//1)點擊頁面中其余的位置(除了文本框和searchList里面的每一行)都隱藏
//2)點擊searchList中的列表隱藏,但是還要把列表中的內容放到文本框中
//->不管是獲取焦點onfocus,還是再里面編輯內容onkeyup,都是有內容顯示,沒內容隱藏
var searchInput = document.getElementById("searchInput")
,searchList = document.getElementById("searchList");
searchInput.onfocus = searchInput.onkeyup = function(e){
console.log(1);
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"){
searchInput.value = e.target.innerHTML;
searchList.style.display = "none";
return;
}
// if(e.target.id === 'searchInput'){
// return;
// }
//或則在input中阻止冒泡
searchList.style.display = "none";
}
//->我們可以阻止一個容器中某些特殊性的元素,讓其不再委托的范圍內
searchInput.onclick = function(e){
e = e || window.event;
e.stopPropagation?e.stopPropagation():e.cancelBubble = true;
}
</script>
</body>
</html>
```
- 空白目錄
- window
- location
- history
- DOM
- 什么是DOM
- JS盒子模型
- 13個核心屬性
- DOM優化
- 回流與重繪
- 未整理
- 文檔碎片
- DOM映射機制
- DOM庫封裝
- 事件
- 功能組件
- table
- 圖片延遲加載
- 跑馬燈
- 回到頂部
- 選項卡
- 鼠標跟隨
- 放大鏡
- 搜索
- 多級菜單
- 拖拽
- 瀑布流
- 數據類型的核心操作原理
- 變量提升
- 閉包(scope)
- this
- 練習題
- 各種數據類型下的常用方法
- JSON
- 數組
- object
- oop
- 單例模式
- 高級單例模式
- JS中常用的內置類
- 基于面向對象創建數據值
- 原型和原型鏈
- 可枚舉和不可枚舉
- Object.create
- 繼承的六種方式
- ES6下一代js標準
- babel
- 箭頭函數
- 對象
- es6勉強筆記
- 流程控制
- switch
- Ajax
- eval和()括號表達式
- 異常信息捕獲
- 邏輯與和或以及前后自增
- JS中的異步編程思想
- 上云
- 優化技巧
- 跨域與JSONP
- 其它跨域相關問題
- console
- HTML、XHTML、XML
- jQuery
- zepto
- 方法重寫和方法重載
- 移動端
- 響應式布局開發基礎
- 項目一:創意簡歷