案例:html/html5 案例     状态:可编辑再运行    进入竖版
 运行结果 
x
 
1
<!DOCTYPE html>
2
<html>
3
<head>
4
<script>
5
function displayResult()
6
{
7
var x;
8
if(window.event) // IE8 以及更早版本
9
    {
10
    x=event.keyCode;
11
    }
12
else if(event.which) // IE9/Firefox/Chrome/Opera/Safari
13
    {
14
    x=event.which;
15
    }
16
keychar=String.fromCharCode(x);
17
alert("按键 " + keychar + " 被按下");
18
}
19
</script>
20
</head>
21
<body>
22
23
<p>当用户在输入字段中按下按键时触发函数。此函数提示用户已按按键。</p>
24
25
<input type="text" onkeypress="displayResult()">
26
27
</body>
28
</html>
29