获得键盘中每个按键的keyCode值,每按下一个按键,页面中table就增加一行,分别显示按下的键和该键代表的keyCode值。代码保存为html文件,浏览器中打开即可
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body id="body">
<script>
function CreateTable(){
}
//创建一个表格,设置表格的样式等
CreateTable.prototype.create=function(){
var table=document.createElement('table');
table.setAttribute('border','1');
table.setAttribute('cellspacing','0');
table.setAttribute('cellpadding','0');
table.setAttribute('align','center');
var thr=document.createElement("tr");
var th=document.createElement("th");
th.innerHTML='键盘按键';
var th2=document.createElement("th");
th2.innerHTML='键盘按键代表的keycode';
thr.appendChild(th);
thr.appendChild(th2);
table.appendChild(thr);
return table;
};
//未表格中添加数据
CreateTable.proto

本文介绍如何通过JavaScript监听键盘事件,获取按下按键对应的keyCode值。实现的功能是,当用户按下键盘上的任意键,页面表格会新增一行,展示所按键的字符及其keyCode。这个例子以HTML文件形式运行在浏览器环境中。
最低0.47元/天 解锁文章
456

被折叠的 条评论
为什么被折叠?



