您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“JavaScript組件開發(fā)之輸入框加候選框的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“JavaScript組件開發(fā)之輸入框加候選框的示例分析”這篇文章吧。
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.對于候選框里面的內(nèi)容使用事件代理,以及點(diǎn)擊空白處消失
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>
以上是“JavaScript組件開發(fā)之輸入框加候選框的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。