問(wèn)題描述
我創(chuàng)建了一個(gè)簡(jiǎn)單的 Web 表單,其中包含一個(gè)文本框和一個(gè)按鈕.我已經(jīng)捕捉到了文本框的onblur事件.
I have created a simple web form, containing one text box and one button. I have captured the onblur event of the text box.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function onTextBoxBlur()
{
alert("On blur");
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server" onblur="onTextBoxBlur();"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
</html>
當(dāng)我在文本框中輸入一些值并單擊按鈕時(shí),會(huì)發(fā)生文本框的 onblur 事件,但不會(huì)發(fā)生按鈕的 onclick 事件.而且,當(dāng)我從 js 函數(shù)中刪除警報(bào)框時(shí),它工作正常.按鈕單擊事件的某些方式丟失了.我認(rèn)為這是由于警報(bào)框.知道為什么會(huì)這樣嗎?
When I enter some value in text box and click on the button, then the onblur event of textbox occurs, but the onclick of the button doesn't. And, when I remove the alert box from the js function then it works fine. Some how the button click is event is lost. I think it is due to the alert box. Any idea why is this so?
推薦答案
一個(gè)按鈕的點(diǎn)擊"有兩個(gè)部分,鼠標(biāo)向下和鼠標(biāo)向上.當(dāng)您將鼠標(biāo)放在按鈕上時(shí),它會(huì)獲得焦點(diǎn) - 模糊文本框并觸發(fā)警報(bào).由于警報(bào)對(duì)話框是模態(tài)的,它們會(huì)暫停頁(yè)面上的所有活動(dòng),因此按鈕不會(huì)檢測(cè)到鼠標(biāo)向上并且您的點(diǎn)擊不會(huì)完成.
A "click" of a button has two parts, mouse down and mouse up. When you mouse down on the button, it gains focus - blurring the text box and firing your alert. Since alert dialogs are modal, they halt all activity on the page so the button doesn't detect the mouse up and your click doesn't complete.
可以使用模糊事件中的計(jì)時(shí)器解決您的問(wèn)題,并在按鈕的 mousedown 事件中取消該計(jì)時(shí)器:
It could be possible to work around your issue using a timer within the blur event, and cancelling that timer within the mousedown event of the button:
var timer;
function onTextBoxBlur()
{
timer = window.setTimeout(function () { alert("On blur"); }, 0);
return true;
}
function onButtonMouseDown()
{
clearTimeout(timer);
}
這篇關(guān)于由于文本框 onblur 事件中的警報(bào)框?qū)е掳粹o單擊事件丟失的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!