1. 对input框中输入的字符进行匹配,将匹配到的内容以菜单的形式展现在 input框的下方; 2. 只针对英文字符进行匹配,并且匹配到的内容在菜单中加粗; 3. 通过键盘上的上下箭头可以对菜单进行选择,按下回车后将选中的内容写入到 input框中;
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>输入框下拉提示</title> <style> *{ margin:0; padding:0; } body{ font-size:14px; font-family:"Microsoft yahei"; } .divCon{ width:300px; margin:100px auto; } #string{ width:200px; height:30px; } #strul{ width:200px; } </style> </head> <body> <form id="inform" name="inform" class="divCon" action="" method="post"> <input id="string" name="string" class="input" type="text/plain" value=""/> </form> <ul id="strul"> </ul> <script type="text/javascript"> window.onload=function(){ var frm=document.getElementById("inform"); var input=document.getElementById("string"); var ul=document.getElementById("strul"); ul.style.position="absolute"; ul.style.top=(input.offsetTop+input.offsetHeight)+"px"; ul.style.left=input.offsetLeft+"px"; var arr=["a","aa","aaa","b","bb","bbb","aab"]; var len=arr.length; //根据输入匹配,显示下拉框 input.selectedIndex=-1; input.focus(); input.onkeyup=function(event){ ul.innerHTML=""; ul.style.borderWidth="1px"; ul.style.borderStyle="solid"; ul.style.borderColor="#999"; var value=input.value; if(value){ var reg=new RegExp("^"+value+"+");//当输入框为空的时候会报错 for(var i=0;i<len;i++){ if(reg.test(arr[i])){ var li=document.createElement("li"); //匹配部分变粗 var matchlen=value.length; var string=arr[i].substr(matchlen); li.innerHTML="<strong>"+value+"</strong>"+string; li.style.listStyle="none"; ul.appendChild(li); } } } //获取所有的li节点 var li=document.getElementsByTagName("li"); for(var j=0,lilen=li.length;j<lilen;j++){ li[j].onmouseover=function(){ this.style.backgroundColor="#ececec"; } li[j].onmouseout=function(){ this.style.backgroundColor="#fff"; } li[j].onclick=function(){ input.value=this.innerText||this.textContent; ul.innerHTML=""; ul.style.border="none"; } } input.options=li; event=event||window.event;//获取事件 //var kc=event.keyCode||event.charCode; switch(event.keyCode){ case 38://上箭头 clearcolor(this); this.selectedIndex--; if(this.selectedIndex<0){ this.selectedIndex=this.options.length-1; } //this.value=this.options[this.selectedIndex].innerHTML; paintcolor(this); break; case 40://下箭头 clearcolor(this); this.selectedIndex++; if(this.selectedIndex>this.options.length-1){ this.selectedIndex=0; } //this.value=this.options[this.selectedIndex].innerHTML; paintcolor(this); break; case 13://回车 if(this.selectedIndex>=0){ var str=this.options[this.selectedIndex].innerHTML; this.value=str.replace(/[<strong><\/strong>]/g,""); } ul.innerHTML=""; ul.style.border="none"; break; } } } function clearcolor(target){ if(target.selectedIndex>=0){ target.options[target.selectedIndex].style.backgroundColor="#fff"; } } function paintcolor(target){ if(target.selectedIndex>=0){ target.options[target.selectedIndex].style.backgroundColor="#ececec"; } } </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> #wrapper{ width:350px; margin:auto; position:relative; } #search{ width:350px; line-height:35px; border:1px solid deepskyblue; display:block; margin:auto; margin-top:100px; } ul,li{ margin:0; padding:0; } ul{ display:none; position:absolute; border:1.5px solid #000; top:38px; left:0; } li{ list-style:none; width:350px; line-height:30px; border-bottom:1px solid grey; } .focus{ background:skyblue; } </style> </head> <body> <div id='wrapper'> <input id='search' type='text' placeholder="请输入查找内容"/> <ul id='result'> </div> </ul> <script> var arr = ['a','aa','aaa','b','bbb','c','aabcd']; var search = document.getElementById('search'); var result = document.getElementById('result'); //拿到焦点的li元素下标 var initial = 0; function searchResult(e){ intial = 0; var e = e || window.event; var str = e.target.value; var keyCode = e.keyCode; var reg = new RegExp(str); var oList = ''; for(let i in arr){ //将匹配的选项拼接成子节点字符串 if(reg.test(arr[i])){ var emStr = '<li><em>'+ str; var restStr = arr[i].substring(str.length+1); oList += emStr +"</em>"+ restStr + '</li>'; } } //s生成列表并显示 result.innerHTML = oList; result.style.display = 'block'; var liList = document.getElementsByTagName('li'); //鼠标移到li元素上加背景 for(var i=0;i<liList.length;i++){ liList[i].index = i; liList[i].onmouseover = function(){ this.style.background = 'skyblue'; initial = this.index; } liList[i].onmouseout = function(){ this.style.background = 'none'; } } e.stopPropagation(); //键盘上移或下移 if(keyCode == 38){ initial--; if(initial<0){ initial = 0; }else{ liList[initial+1].className = ''; } }else if(keyCode == 40){ initial++; console.log(initial) if(initial > liList.length-1){ initial = liList.length-1; }else{ liList[initial-1].className = ''; console.log('ss') } } liList[initial].className = 'focus'; //监听enter键 if(keyCode==13){ this.value = liList[initial].innerText; result.style.display = 'none'; } } //只能keyup,不然第一次不到要匹配字符串!!! search.onkeyup = searchResult; </script> </body> </html>