主要就是为了监听键盘按下的是哪个键,并且转成ASCII码;
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
>
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
< title > window.event.keyCode </ title >
</ head >
< body onkeydown =" alert(window.event.keyCode)" >
< input id ="txtCode" type ="text" />
</ body >
</ html >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
< title > window.event.keyCode </ title >
</ head >
< body onkeydown =" alert(window.event.keyCode)" >
< input id ="txtCode" type ="text" />
</ body >
</ html >
那么如何用回车代替小键盘的回车呢?首先我们要知道,回车的ASCII码是13 ,TAB键的ASCII码是9那就好办啦;
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
>
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
< title > 使用回车代替TAB键 </ title >
</ head >
< body onkeydown ="if(window.event.keyCode==13){ window.event.keyCode=9}" >
< input id ="txtCode" type ="text" />
< input id ="txtCode2" type ="text" />
</ body >
</ html >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
< title > 使用回车代替TAB键 </ title >
</ head >
< body onkeydown ="if(window.event.keyCode==13){ window.event.keyCode=9}" >
< input id ="txtCode" type ="text" />
< input id ="txtCode2" type ="text" />
</ body >
</ html >
在上面的代码中我们使用了keyCode来实现回车代替TAB键,只是简单的做了一个判断,如果当按下的是回车(13),我们就把TAB(9)的值赋给它(骗骗计算机),这些小小的细节积累起来,也是一批不小的财富吧!
下面接着写点东西,算是一个完整的示例吧;一个金融软件中文本框只能输入数字,和方向键,删除键等(代码中不是很严谨,仅供参考)
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< title > 金融软件文本框禁用非法字符特效-2011-04-26 </ title >
< style type ="text/css" >
.ime
{
ime-mode : disabled ; /* *只对IE有效,禁用输入法切换* */
width : 300px ;
}
</ style >
< script language ="javascript" type ="text/javascript" >
function isValiNum(k) {
return (k == 9 || k == 13 || k == 46 || k == 8 || k == 189 || k == 190 || k == 110 || (k >= 48 && k <= 57 ) || (k >= 96 && k <= 105 ) || (k >= 37 && k <= 40 ));
}
function numKeyDown() {
var k = window.event.keyCode;
// 判断输入字符是否合法;调用isValiNum函数;
return isValiNum(k);
}
function numPaste() {
// 从剪切板中获取数据;
var text = window.clipboardData.getData( " Text " );
for ( var i = 0 ; i < text.length; i ++ ) {
var asc = text.charCodeAt(i); // 转成ASCII值;
if ( ! isValiNum(asc)) {
return false ;
}
}
}
// 自动给输入的数据加逗号;
function commafy(n) {
var re = / \d{1,3}(?=(\d{3})+$) / g;
var n1 = n.replace( / ^(\d+)((\.\d+)?)$ / , function (s, s1, s2) { return s1.replace(re, " $&, " ) + s2; });
return n1;
}
</ script >
</ head >
< body >
< input id ="txtCode" type ="text" class ="ime" onpaste ="return numPaste()" onkeydown ="return numKeyDown()" onfocus ="this.style.textAlign='left'; this.value=this.value.replace(/,/g,'');" onblur ="this.style.textAlign='right';this.value=commafy(this.value)" />
</ body >
</ html >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< title > 金融软件文本框禁用非法字符特效-2011-04-26 </ title >
< style type ="text/css" >
.ime
{
ime-mode : disabled ; /* *只对IE有效,禁用输入法切换* */
width : 300px ;
}
</ style >
< script language ="javascript" type ="text/javascript" >
function isValiNum(k) {
return (k == 9 || k == 13 || k == 46 || k == 8 || k == 189 || k == 190 || k == 110 || (k >= 48 && k <= 57 ) || (k >= 96 && k <= 105 ) || (k >= 37 && k <= 40 ));
}
function numKeyDown() {
var k = window.event.keyCode;
// 判断输入字符是否合法;调用isValiNum函数;
return isValiNum(k);
}
function numPaste() {
// 从剪切板中获取数据;
var text = window.clipboardData.getData( " Text " );
for ( var i = 0 ; i < text.length; i ++ ) {
var asc = text.charCodeAt(i); // 转成ASCII值;
if ( ! isValiNum(asc)) {
return false ;
}
}
}
// 自动给输入的数据加逗号;
function commafy(n) {
var re = / \d{1,3}(?=(\d{3})+$) / g;
var n1 = n.replace( / ^(\d+)((\.\d+)?)$ / , function (s, s1, s2) { return s1.replace(re, " $&, " ) + s2; });
return n1;
}
</ script >
</ head >
< body >
< input id ="txtCode" type ="text" class ="ime" onpaste ="return numPaste()" onkeydown ="return numKeyDown()" onfocus ="this.style.textAlign='left'; this.value=this.value.replace(/,/g,'');" onblur ="this.style.textAlign='right';this.value=commafy(this.value)" />
</ body >
</ html >