onclick和onblur沖突問題的快速解決方法
新浪首頁的搜索框里面有一個(gè)使用ajax的下拉框。我們需要實(shí)現(xiàn)一個(gè)點(diǎn)擊下拉框里面的一項(xiàng),讓搜索框里面的值變成這一項(xiàng),同時(shí)下拉框消失的效果,但同時(shí)在點(diǎn)擊其他地方的時(shí)候,這個(gè)下拉框也要消失。大致如圖:

我們同時(shí)使用onblur和onclick來使下拉框隱藏,但是更大的問題出現(xiàn)了,這兩個(gè)功能相沖突,onblur過于強(qiáng)悍,根本沒有onclick方法實(shí)現(xiàn)的機(jī)會(huì),搜索框無法獲取點(diǎn)擊項(xiàng)的內(nèi)容。這個(gè)就是我們想要解決的onclick和onblur沖突問題。
對(duì)應(yīng)這個(gè)問題,這里我們介紹兩種解決辦法:
1. 使用setTimeout來使onblur時(shí)間延期執(zhí)行,使onclick執(zhí)行完后再執(zhí)行onblur。(其中setTimeout的時(shí)間設(shè)定應(yīng)該在100ms以上,否則依舊不行)示例代碼如下:
<!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='請(qǐng)輸入關(guān)鍵字';
}
},120);
};
};
</script>
</head>
<body>
<form>
<input type="text" value="請(qǐng)輸入關(guān)鍵字" id="text" class="text"/>
<input type="button" value="搜索" class="button"/>
<ul id="ul">
<li>返回窗口是否已被關(guān)閉</li>
<li>返回窗口的文檔顯示區(qū)的高度</li>
<li>返回窗口的文檔顯示區(qū)的寬度。</li>
<li>設(shè)置或返回窗口的名稱。</li>
<li>返回窗口的外部高度。</li>
</ul>
</form>
</body>
</html>
2. 使用document.onmousedown來代替onblur實(shí)現(xiàn)隱藏下拉框功能
<!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='請(qǐng)輸入關(guān)鍵字';
}
};
};
</script>
</head>
<body>
<form>
<input type="text" value="請(qǐng)輸入關(guān)鍵字" id="text" class="text"/>
<input type="button" value="搜索" class="button"/>
<ul id="ul">
<li>返回窗口是否已被關(guān)閉</li>
<li>返回窗口的文檔顯示區(qū)的高度</li>
<li>返回窗口的文檔顯示區(qū)的寬度。</li>
<li>設(shè)置或返回窗口的名稱。</li>
<li>返回窗口的外部高度。</li>
</ul>
</form>
</body>
</html>
以上這篇onclick和onblur沖突問題的快速解決方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
原生JavaScript實(shí)現(xiàn)簡單五子棋游戲
這篇文章主要為大家詳細(xì)介紹了原生JavaScript實(shí)現(xiàn)簡單五子棋游戲,文中示例代碼注釋的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
微信小程序?qū)崿F(xiàn)走馬燈式抽獎(jiǎng)
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)走馬燈式抽獎(jiǎng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
js創(chuàng)建元素(節(jié)點(diǎn))示例
本文為大家介紹下使用js是如何創(chuàng)建元素(節(jié)點(diǎn))的,感興趣的朋友不要錯(cuò)過2014-01-01
JavaScript創(chuàng)建對(duì)象的七種經(jīng)典方式分享
JavaScript 創(chuàng)建對(duì)象的方式有很多,通過 Object 構(gòu)造函數(shù)或?qū)ο笞置媪康姆绞揭部梢詣?chuàng)建單個(gè)對(duì)象,顯然這兩種方式會(huì)產(chǎn)生大量的重復(fù)代碼,并不適合量產(chǎn)。本文介紹了七種非常經(jīng)典的創(chuàng)建對(duì)象的方式,希望對(duì)大家有所幫助2022-11-11
使用apply方法處理數(shù)組的三個(gè)技巧[譯]
本文要講的是使用apply方法處理數(shù)組的三個(gè)技巧,需要的朋友可以參考下2012-09-09
JS面向?qū)ο缶幊虒?shí)現(xiàn)的Tab選項(xiàng)卡案例詳解
這篇文章主要介紹了JS面向?qū)ο缶幊虒?shí)現(xiàn)的Tab選項(xiàng)卡,結(jié)合具體案例形式詳細(xì)分析了JS基于面向?qū)ο蟪绦蛟O(shè)計(jì)實(shí)現(xiàn)Tab選項(xiàng)卡的相關(guān)操作技巧,需要的朋友可以參考下2020-03-03
webpack?打包后圖片加載報(bào)錯(cuò)的解決辦法
使用webpack打包后,圖片沒有加載出來,頁面空白,報(bào)錯(cuò)圖片引用的路徑不對(duì),本文給大家介紹webpack?打包后圖片加載報(bào)錯(cuò)的解決辦法,感興趣的朋友一起看看吧2024-03-03

