
javaScript 中如何实现 随意按下一个键 ,然后返回按下键上面对应的字符
4个回答
展开全部
楼上各位,楼主问的是按键盘上的键,不是按按钮。我来写一个吧。
兼容各主流浏览器(按键是无法区分大小写的,统一作为大写处理的)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>获取按键</title>
<style>
</style>
<script>
function exec() {
var ff = document.body.innerText == undefined;
document.onkeydown = function(e) {
e = e ? e : window.event;
var key = String.fromCharCode(e.keyCode); // 这个就是按键了
if (ff)
document.body.textContent += key;
else
document.body.innerText += key;
};
}
</script>
</head>
<body onload="exec()">
</body>
</html>
兼容各主流浏览器(按键是无法区分大小写的,统一作为大写处理的)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>获取按键</title>
<style>
</style>
<script>
function exec() {
var ff = document.body.innerText == undefined;
document.onkeydown = function(e) {
e = e ? e : window.event;
var key = String.fromCharCode(e.keyCode); // 这个就是按键了
if (ff)
document.body.textContent += key;
else
document.body.innerText += key;
};
}
</script>
</head>
<body onload="exec()">
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果是想知道按钮值<input type="button" value="这个按钮" onclick="alert (this.value);"/>
如果是想知道键盘按下的键的值<body onkeydown="alert('你按下了' + event.keyCode +'');">
如果是想知道键盘按下的键的值<body onkeydown="alert('你按下了' + event.keyCode +'');">
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<script type="text/javascript">
function expample(id){
alert(document.getElementById(id).value);
}
</script>
<body>
<button id="b1" onclick="expample('b1')" value="按钮1">按钮1</button>
<button id="b2" onclick="expample('b2')" value="按钮2">按钮2</button>
<button id="b3" onclick="expample('b3')" value="按钮3">按钮3</button>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<script type="text/javascript">
function expample(id){
alert(document.getElementById(id).value);
}
</script>
<body>
<button id="b1" onclick="expample('b1')" value="按钮1">按钮1</button>
<button id="b2" onclick="expample('b2')" value="按钮2">按钮2</button>
<button id="b3" onclick="expample('b3')" value="按钮3">按钮3</button>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个js的代码还是比较大的,我前段时间收集了,不过现在不知道房子啊哪里了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询