There is a drop-down box using ajax in the search box on Sina homepage. We need to achieve the effect of clicking an item in the drop-down box so that the value in the search box changes to this item and the drop-down box disappears. But at the same time, when clicking elsewhere, the drop-down box also disappears. Approximately as shown in the picture:
We use onblur and onclick at the same time to hide the drop-down box, but a bigger problem arises. These two functions conflict. Onblur is too powerful and there is no chance of implementing the onclick method. The search box cannot obtain the content of the clicked item. This is the conflict between onclick and onblur that we want to solve.
Corresponding to this problem, here we introduce two solutions:
1. Use setTimeout to delay the execution of onblur time, so that onblur is executed after onclick is executed. (The time setting of setTimeout should be above 100ms, otherwise it still won’t work) The sample code is as follows:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> *{ margin: 0; padding: 0; list-style: none; } form{ width:500px; margin:0 auto; position:relative; zoom:1; } form:after{ clear:both; content:""; display:block; } .text{ float:left; border:1px solid #cccccc; padding-left:14px; width:300px; height:34px; line-height:34px; font-size:14px; } .button{ width:50px; height:34px; border:1px solid #cccccc; line-height:34px; font-size:14px; color:#ffffff; background:#ff8400; } ul{ position:absolute; top:36px; left:0; width:300px; border-right:1px solid #cccccc; border-left:1px solid #cccccc; background:green; display:none; } li{ font-size:14px; line-height:34px; height:34px; color:#000000; border-bottom:1px solid #cccccc; } li:hover{ background:yellow; color:red; cursor:pointer; } </style> <script> window.onload=function(){ var oText=document.getElementById('text'); var oUl=document.getElementById('ul'); var aLi=oUl.getElementsByTagName('li'); var timer=null; oText.onfocus=function(){ this.value=''; oUl.style.display='block'; for(var i=0;i<aLi.length;i++){ aLi[i].onclick=function(){ clearTimeout(timer); oText.value=this.innerHTML; oUl.style.display='none'; }; } }; oText.onblur=function(){ timer=setTimeout(function(){ oUl.style.display='none'; if(!oText.value){ oText.value='请输入关键字'; } },120); }; }; </script> </head> <body> <form> <input type="text" value="请输入关键字" id="text" class="text"/> <input type="button" value="搜索" class="button"/> <ul id="ul"> <li>返回窗口是否已被关闭</li> <li>返回窗口的文档显示区的高度</li> <li>返回窗口的文档显示区的宽度。</li> <li>设置或返回窗口的名称。</li> <li>返回窗口的外部高度。</li> </ul> </form> </body> </html>
2. Use document.onmousedown instead of onblur to hide the drop-down box function
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> *{ margin: 0; padding: 0; list-style: none; } form{ width:500px; margin:0 auto; position:relative; zoom:1; } form:after{ clear:both; content:""; display:block; } .text{ float:left; border:1px solid #cccccc; padding-left:14px; width:300px; height:34px; line-height:34px; font-size:14px; } .button{ width:50px; height:34px; border:1px solid #cccccc; line-height:34px; font-size:14px; color:#ffffff; background:#ff8400; } ul{ position:absolute; top:36px; left:0; width:300px; border-right:1px solid #cccccc; border-left:1px solid #cccccc; background:green; display:none; } li{ font-size:14px; line-height:34px; height:34px; color:#000000; border-bottom:1px solid #cccccc; } li:hover{ background:yellow; color:red; cursor:pointer; } </style> <script> window.onload=function(){ var oText=document.getElementById('text'); var oUl=document.getElementById('ul'); var aLi=oUl.getElementsByTagName('li'); var timer=null; oText.onfocus=function(){ this.value=''; oUl.style.display='block'; for(var i=0;i<aLi.length;i++){ aLi[i].onclick=function(){ clearTimeout(timer); oText.value=this.innerHTML; oUl.style.display='none'; }; } }; document.onmousedown=function(ev){ var oEvent=ev||event; var target=oEvent.target||oEvent.srcElement; if(target.parentNode!==oUl&&target!==oText){ oUl.style.display='none'; } }; oText.onblur=function(){ if(!oText.value){ oText.value='请输入关键字'; } }; }; </script> </head> <body> <form> <input type="text" value="请输入关键字" id="text" class="text"/> <input type="button" value="搜索" class="button"/> <ul id="ul"> <li>返回窗口是否已被关闭</li> <li>返回窗口的文档显示区的高度</li> <li>返回窗口的文档显示区的宽度。</li> <li>设置或返回窗口的名称。</li> <li>返回窗口的外部高度。</li> </ul> </form> </body> </html>
The quick solution to the onclick and onblur conflict problem mentioned above is all the content shared by the editor. I hope it can give you a reference, and I hope you will support Script Home.