event.keyCode|| event.which.的用法

 

HTML

 用户名:<input type="text" id="UserAccount" onKeyPress="JumpByEnter(UserPwd)" />

 密码:<input name="UserPwd" type="password" onKeyPress="IsEnterKeyPress()">

 JavaScript:

JumpByEnter(UserPwd) 函数功能:输入完用户名按回车,焦点转向密码输入框

IsEnterKeyPress()功能:输入完密码并按回车,则登录

 function JumpByEnter(NextElement){
     var lKeyCode = (navigator.appname=="Netscape")?event.which:window.event.keyCode; //event.keyCode按的建的代码,13表示回车
     if ( lKeyCode == 13 ){

 

    NextElement.focus();
     }

 function IsEnterKeyPress(){
     var lKeyCode = (navigator.appname=="Netscape")?event.which:event.keyCode;
     if ( lKeyCode == 13 ){
       Login();
     }
     else
       return false;
   }

 irfirefox2.0中不支持 window.event.keyCode,

 但是我们可以用event.which代替。但是为了使其能更具有普遍的兼容性,最好用event.keyCode|| event.which.

用法举例:C#后台代码:txtHampooPN.Attributes.Add("onkeydown", "if(event.keyCode == 13 && " + txtHampooPN.ClientID + ".value.length ==0)return false; ");

 txtPinyinCode.Attributes.Add("onkeydown", "if(event.which||event.keyCode){if ((event.which==13 ) || (event.keyCode==13)) { document.getElementById('" + btnLocation.UniqueID + "').click();return false;}}else{return true};");

键码值参考:http://www.cnblogs.com/hsapphire/archive/2009/12/16/1625642.html

 

 

你可能感兴趣的:(keycode)