javascript键盘事件

<html><head><metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/><title>... <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript" type="text/javascript">
function keyContent(e) {
if (!e) e = window.event;
if (e.keyCode) keyCode = e.keyCode;
else keyCode = e.which;
character = String.fromCharCode(keyCode);
k = document.getElementById("keys");
k.innerHTML += character;
}
window.onload = function() {
k = document.getElementById("keys");
if (k.addEventListener) {
k.addEventListener('keypress', keyContent, false);
}
else if (k.attachEvent) {
k.attachEvent('onkeypress', keyContent);
}
}
</script>
</head>
<body>
<p>Displaying Typed Characters</p>
<p>This document includes a simple script that displays the keys you type in the paragraph below. Type a few keys and try it. </p>
<p id="keys" onKeyPress="keyContent(event);">
</p>
</body>
</html>
无法实现键盘事件,求解?
471348579,请问一下document.onkeypress=keyContent;这条语句改成
window.onload = function() {
k = document.getElementById("keys");
if (k.addEventListener) {
k.addEventListener('keypress', keyContent, false);
}
else if (k.attachEvent) {
k.attachEvent('onkeypress', keyContent);
}
}
为什么不行?我是用遨游运行的。答案满意,追加分数!
展开
 我来答
百度网友a435633
2010-01-28 · TA获得超过1234个赞
知道大有可为答主
回答量:1018
采纳率:0%
帮助的人:0
展开全部
首先
<p id="keys" onKeyPress="keyContent(event);">
这句的 onKeyPress 多余了,因为上面的 JS 代码已经给他绑定。
其次,因为这个 <p> 不可编辑,所以不会产生 keypress 事件。

给你改一下程序,监听 window 的 keypress,显示在指定区域。

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>JavaScript测试</title>
<script language="javascript" type="text/javascript">
function keyContent(e) {
if (!e) e = window.event;
if (e.keyCode) keyCode = e.keyCode;
else keyCode = e.which;
character = String.fromCharCode(keyCode);
k = document.getElementById("keys");
k.innerHTML += character;
}
window.onload = function() {
k = window;
if (k.addEventListener) {
k.addEventListener('keypress', keyContent, false);
}
else if (k.attachEvent) {
k.attachEvent('onkeypress', keyContent);
}
}
</script>
</head>
<body>
<p>Displaying Typed Characters</p>
<p>This document includes a simple script that displays the keys you type in the paragraph below. Type a few keys and try it. </p>
<p id="keys" style="border: 1px solid #FF0000; width:300px; height:200px;">
</p>
</body>
</html>
niubore
2010-01-27 · TA获得超过856个赞
知道小有建树答主
回答量:293
采纳率:0%
帮助的人:306万
展开全部
小小的改动了下。onKeyPress只支持有键盘输入的<input>元素

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript" type="text/javascript">
function keyContent(e) {
if (!e) e = window.event;
if (e.keyCode) keyCode = e.keyCode;
else keyCode = e.which;
character = String.fromCharCode(keyCode);
k = document.getElementById("keys");
k.innerHTML += character;
}
window.onload = function() {
k = document.getElementById("keys");
if (k.addEventListener) {
k.addEventListener('keypress', keyContent, false);
}
else if (k.attachEvent) {
k.attachEvent('onkeypress', keyContent);
}
}
</script>
</head>
<body>
<p>Displaying Typed Characters</p>
<p>This document includes a simple script that displays the keys you type in the paragraph below. Type a few keys and try it. </p>
<p id="keys" >
</p>
在这里输入<input type="text" onKeyPress="keyContent(event);">
</body>
</html>
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
帐号已注销
推荐于2016-06-26 · TA获得超过1232个赞
知道大有可为答主
回答量:1245
采纳率:0%
帮助的人:1424万
展开全部
小改动看看效果

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript" type="text/javascript">
function keyContent(e) {
var e=e||event;
var keyCode=e.keyCode?e.keyCode:e.which;
character = String.fromCharCode(keyCode);
k = document.getElementById("keys");
k.innerHTML += character;
}
document.onkeypress=keyContent;
</script>
</head>
<body>
<p>Displaying Typed Characters</p>
<p>This document includes a simple script that displays the keys you type in the paragraph below. Type a few keys and try it. </p>
<p id="keys"></p>
</body>
</html>
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式