选项卡丢失焦点事件的Javascript

时间:2012-07-12 07:42:23

标签: c# javascript asp.net

我有一个像这样的javascript函数:

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "watermark", "function WaterMark(txtWaterMark, event, text) {if(event.type == 'keyup') {var charCode; if (event.charCode) { charCode = event.charCode;} else { charCode = event.keyCode; } if(charCode == 9 && txtWaterMark.value.length == 0) txtWaterMark.value = 'Pakistan';}  if (txtWaterMark.value.length > 0 && event.type == 'mouseover') { txtWaterMark.style.color = '#c6c1c1'; } if (txtWaterMark.value.length > 0 && event.type == 'mouseout') { txtWaterMark.style.color = 'gray';} if (txtWaterMark.value.length == 0 && event.type == 'mouseout') {txtWaterMark.value = text; }  if (event.type == 'focus') {txtWaterMark.style.color = 'gray'; if (txtWaterMark.value == text) { txtWaterMark.value = ''; } }}", true);

我称之为:

  <asp:TextBox ID="txtFirstName" runat="server" Width="110px" Text="First Name."
                ForeColor="Gray" onmouseover="WaterMark(this, event,'First Name.');" onmouseout="WaterMark(this, event,'First Name.');"
                onfocus="WaterMark(this, event,'First Name.');" onkeyup="WaterMark(this, event,'First Name.');" ToolTip="First Name."
                ValidationGroup="CheckoutConfirm"></asp:TextBox>

我正在使用keyup事件,这样如果用户按Tab键在文本框之间移动,则会在文本框中添加一个文本,但是当按下tab后,当焦点位于文本框内时,会触发keyup。我想要一些像'失去焦点'这样的事件我尝试过模糊但没有任何好处。

1 个答案:

答案 0 :(得分:1)

当用户离开输入字段时,javascript中会出现模糊事件。

http://www.w3schools.com/jsref/event_onblur.asp