1、应了解,实现方式,即只要键盘按键按下并释放,就会进行判断是否为enter键。
2、了解如何捕获enter键。
*******************************************jsp************************************************
<tr>
<td ><span>登录账号:</span></td>
<td colspan="2"><input type="text" id="login_username" placeholder="登录账号"onKeyPress="IsEnterKeyPress()"></td>
</tr>
<tr>
<td ><span>登录密码:</span></td>
<td colspan="2"><input type="password" id="login_password" placeholder="登录密码"onKeyPress="IsEnterKeyPress()"></td>
</tr>
<tr>
<td ><span>验 证 码:</span></td>
<td ><input type="text" id="login_validcode" placeholder="验证码"onKeyPress="IsEnterKeyPress()"></td>
</tr>
*****************************************js***********************************************
function IsEnterKeyPress(){
var lKeyCode = (navigator.appname=="Netscape")?event.which:window.event.keyCode;
if ( lKeyCode == 13 ){
//执行代码
}
}
*****************************************注意*************************************************
浏览器差异:Internet
Explorer 使用 event.keyCode 取回被按下的字符,而 Netscape/Firefox/Opera 使用 event.which。
keyCode属性 对于 keypress 事件,该属性声明了被敲击的键生成的 Unicode 字符码。对于 keydown 和 keyup 事件,它指定了被敲击的键的虚拟键盘码。虚拟键盘码可能和使用的键盘的布局相关。
Event 对象代表事件的状态,比如事件在其中发生的元素、键盘按键的状态、鼠标的位置、鼠标按钮的状态。
事件通常与函数结合使用,函数不会在事件发生前被执行!
本文详细介绍了如何在HTML页面中监听键盘事件,特别是针对Enter键的处理方法,并通过JavaScript实现了基本的输入验证功能。文章包括了不同浏览器对键盘事件的不同处理方式,以及关键的代码实现步骤。
684

被折叠的 条评论
为什么被折叠?



