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);
}
}
为什么不行?我是用遨游运行的。答案满意,追加分数! 展开
<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);
}
}
为什么不行?我是用遨游运行的。答案满意,追加分数! 展开
展开全部
首先
<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>
<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>
展开全部
小小的改动了下。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>
<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>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
小改动看看效果
<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>
<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>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询