JavaScript組件開發之輸入框加候選框,javascript組件

來源:互聯網
上載者:User

JavaScript組件開發之輸入框加候選框,javascript組件

1.相容ie8 主要是事件相容

var EventUtil = {   on:function(elem,type,handler){    if(elem.addEventListener){     elem.addEventListener(type,handler,false);    }else if(elem.attachEvent){     elem.attachEvent("on"+type,handler);    }   },   getEvent:function(event){    return event||window.event;   },   getTarget:function(event){    return event.target||event.srcElement;   },   getCharCode:function(event){    if(typeof event.handler == "number"){     return event.charCode;    }else{     return event.keyCode;    }   }  }

2.對於候選框裡面的內容使用事件代理,以及點擊空白處消失

EventUtil.on(document.body,'click',function(e){   stopPropagation(e);   if(EventUtil.getTarget(e).nodeName=='BODY'){    datalist.style.visibility = 'hidden';    datalist.innerHTML ='';   }   if(EventUtil.getTarget(e).nodeName == "LI"){    input.value = EventUtil.getTarget(e).innerHTML;    datalist.style.visibility = 'hidden';    datalist.innerHTML ='';   }  })

3.相容模式下的防止冒泡

 function stopPropagation(e){   e = window.event||e;   if(document.all){    e.cancelBubble = true;   }else{    e.stopPropagation();   }  }

4.

 

5.完整代碼

<!DOCTYPE html><html> <head>  <meta charset="utf-8">  <title></title>  <style media="screen">   html,body{margin: 0;padding: 0;height: 100%;width: 100%;}   input{    width: 200px;    border:1px solid grey;    padding: 0 2px;    line-height: 1.5rem;    box-sizing: border-box;    outline: none;   }   ul{    margin:0;    width: 200px;    padding: 0;    list-style: none;    box-sizing: border-box;    padding: 1px;    border:1px solid;    border-color: grey;    visibility: hidden;   }   li{    line-height: 1.5rem;    padding: 0 0 0 1px;   }   li:hover{    background-color: grey;   }   .section{    top:30%;    left:50%;    position: absolute;    margin-left: -100px;   }  </style> </head> <body> <div class="section">   <input id="search"/>   <ul id="datalist">   </ul> </div> </body> <script type="text/javascript">  var EventUtil = {   on:function(elem,type,handler){    if(elem.addEventListener){     elem.addEventListener(type,handler,false);    }else if(elem.attachEvent){     elem.attachEvent("on"+type,handler);    }   },   getEvent:function(event){    return event||window.event;   },   getTarget:function(event){    return event.target||event.srcElement;   },   getCharCode:function(event){    if(typeof event.handler == "number"){     return event.charCode;    }else{     return event.keyCode;    }   }  }  function stopPropagation(e){   e = window.event||e;   if(document.all){    e.cancelBubble = true;   }else{    e.stopPropagation();   }  }  var input = document.getElementById('search');  var datalist = document.getElementById('datalist');  var list_array = ['aa','aab','abc'];  function generatelist(array){   var _innerHTML = '';   for (var i = 0; i < array.length; i++) {    _innerHTML += '<li>'+array[i]+'</li>';   }   datalist.innerHTML = _innerHTML;  }  function findInArray(s){   var filter_array = [];   if(s!=''){    for (var i = 0; i < list_array.length; i++) {     if(list_array[i].indexOf(s)===0){      filter_array.push(list_array[i])     }    }   }   return filter_array;  }  input.onkeyup = function(){   var new_array = findInArray(this.value);   generatelist(new_array);   if(new_array.length>0){    setTimeout(function(){datalist.style.visibility = 'visible';},0);   }else{    setTimeout(function(){datalist.style.visibility = 'hidden';},0);   }  }  EventUtil.on(document.body,'click',function(e){   stopPropagation(e);   if(EventUtil.getTarget(e).nodeName=='BODY'){    datalist.style.visibility = 'hidden';    datalist.innerHTML ='';   }   if(EventUtil.getTarget(e).nodeName == "LI"){    input.value = EventUtil.getTarget(e).innerHTML;    datalist.style.visibility = 'hidden';    datalist.innerHTML ='';   }  }) </script></html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.