javascript当中 document onkeydown的用法
例 2.2(documentKeypressIEFF.html)
马克-to-win:当系统看见这句话:document.onkeydown = handleKeypress; 以后,当你按keydown时,系统自然就调用: handleKeypress(event)。而且传进来event参数。马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</HEAD>
<script>
function handleKeypress(event)
{
/*火狐用event所以window.event为undefined,ie正相反,所以event || window.event可以兼容*/
alert("event is "+event +"window.event is "+window.event);
alert(111||undefined);//任何数和undefined做||,为原值。
var event = event || window.event;
if (window.navigator.userAgent.indexOf("MSIE") >= 1)
{
var key = event.keyCode;
alert("Key: " + String.fromCharCode(key) + "\nCharacter code: " + key + ".");
}
else if ( window.navigator.userAgent.indexOf("Firefox") >= 1)
{ var key = event.which;//event.which获取按下的键盘按键Unicode值:
/*fromCharCode() 可接受一个或n个指定的 Unicode 值,然后返回一个或多个字符*/
alert("Key: " + String.fromCharCode(key) + "\nCharacter code: " + key + ".");
}
}
document.onkeydown = handleKeypress;
</script>
</HTML>