在做登陆页面时,当输入密码后,按回车键实现鼠标点击登陆按钮的效果
代码如下:
<ul class="form_login">
<li>
<label for="txtUserName">
用户名</label>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
</li>
<li>
<label for="txtPassWord">
密 码</label>
<asp:TextBox ID="txtPassWord" runat="server" TextMode="Password"></asp:TextBox>
</li>
</ul>
<ul style="padding-left: 83px;">
<li>
<asp:CheckBox ID="systemAdmon" runat="server" />
<label>
系统管理员</label>
</li>
</ul>
<ul style="padding-left: 88px; margin-top: 10px;">
<li>
<label>
</label>
<asp:LinkButton runat="server" ID="lbLogin" Text="登录" CssClass="button_ids loginButton"
OnClick="lbLogin_Click"></asp:LinkButton>
<asp:LinkButton runat="server" ID="lbLoginCancel" Text="取消" CssClass="button_ids loginButton"></asp:LinkButton></li>
</ul>
JS代码实现的效果。代码如下:
<script type="text/javascript">
function $(id)
{
return document.getElementById(id);
}
function fire(e,buttonId){
var theEvent = window.event || e;
if (theEvent.keyCode==13)
{
//避免firefox下响应焦点事件 ----------------
//选择parentNode是按钮避免重复响应事件 ----------------
$(buttonId).parentNode.focus();
if($(buttonId)){
if(window.attachEvent && !window.opera)
{
$(buttonId).click();
}
else
{
var evt = document.createEvent("MouseEvents");
evt.initEvent("click", true, false);
$(buttonId).dispatchEvent(evt);
}
}
}
}
document.onkeydown = function(e)
{
fire(e,'lbLogin');
}
</script>