找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 10324|回复: 1

js判断按键内容

[复制链接]
发表于 2009-11-30 02:33:10 | 显示全部楼层 |阅读模式 IP:江苏扬州
<!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>
test
</title>
</head>

<script>
function check(e){
var e = window.event ? window.event : e;
    if(e.keyCode == 9){
        alert("You press the \"Tab\" button");
    }
}
</script>
<body>
<p>
<input type="text" name="textfield" onkeydown="check(event);" />
</p>
<p>
<input type="text" name="textfield2" />
</p>
</body>
</html>

判断是否按下了“F5”
document.onkeydown = function(e){
var e = window.event ? window.event : e;
if(e.keyCode == 116){
alert("按下了F5")
}
}

ASCII码


ASCII码键盘ASCII 码键盘ASCII 码键盘ASCII 码键盘
27ESC32SPACE33!34"
35#36$37%38&
39'40(41)42*
43+44'45-46.
47/480491502
513524535546
55756857958:
59;60<61=62>
63?64@65A66B
67C68D69E70F
71G72H73I74J
75K76L77M78N
79O80P81Q82R
83S84T85U86V
87W88X89Y90Z
91[92\93]94^
95_96`97a98b
99c100d101e102f
103g104h105i106j
107k108l109m110n
111o112p113q114r
115s116t117u118v
119w120x121y122z
123{124|125}126~


<!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>
<script>
var gridw=15;
var rows=30;
var cols=30;
//var mw=300;
//var mh=300;
var mw=rows*gridw;
var mh=cols*gridw;
var kw=15
var kh=15;
var kx=0;
var ky=0;
var fw=15
var fh=15;
var fx=0;
var fy=0;
var spet=15;
var map=new Array();
function init(){
createmap();
createfood();
createk();
alldiv=document.getElementById("map").getElementsByTagName("div");
}
function test2(){//随机一定范围内指定数的倍数
xxx=parseInt(Math.random()*100000000000)
while(xxx%15!=0){
xxx=parseInt(Math.random()*100000000000)
}
document.getElementById("cont").innerHTML=xxx
}
function createmap(){
document.body.innerHTML+="<div id='map' style=' width:"+mw+"; height:"+mh+"; border:#000000 solid 1px; margin:0 auto; position:relative '></div>";
for(y=0;y<rows;y++){
map[y]=new Array()
for(x=0;x<cols;x++){
map[y][x]="0";
}
}
//alert(map[8][15])
}
function cleardiv(){
var a6=document.getElementById("food");
mm=document.getElementById("map");
mm.removeChild(a6)
//alert("Clear")
}
function createfood(){
fx=parseInt(Math.random()*cols)
fy=parseInt(Math.random()*rows)
/*while(fx%15!=0){
fx=parseInt(Math.random()*cols)
}
while(fy%15!=0){
fy=parseInt(Math.random()*rows)
}*/
if(map[fy][fx]=="0"){
document.getElementById("map").innerHTML+="<span id='food' style=' width:"+fw+"; height:"+fh+"; background-color:#000000; position:absolute; top:"+fx*gridw+"; left:"+fy*gridw+" '></span>"
map[fy][fx]="f";
}else{
createfood();
}
a1.innerHTML="fx:"+fx+"   fy:"+fy
//alert(map[fy][fx])
}
function createk(){
document.getElementById("map").innerHTML+="<div id='k' style=' width:"+kw+"; height:"+kh+"; background-color:#FF0000; position:absolute; top:"+kx+"; left:"+ky+"'></div>"
//map[ky][kx]="k"
}
function move(x,y){
var hero=document.getElementById("k");
if(parseInt(hero.style.left)==0){
if(x<0){
x=0
}
}
if(parseInt(hero.style.left)==mw-kw){
if(x>0){
x=0
}
}
if(parseInt(hero.style.top)==0){
if(y<0){
y=0
}
}
if(parseInt(hero.style.top)==mh-kh){
if(y>0){
y=0
}
}
hero.style.left=parseInt(hero.style.left)+x*spet;
hero.style.top=parseInt(hero.style.top)+y*spet;
mkx=parseInt(alldiv[0].style.left)/gridw
mky=parseInt(alldiv[0].style.top)/gridw
if(alldiv.length>1){
dd()
}
map[mkx][mky]="k"
if(map[mkx][mky]==map[fy][fx]){
cleardiv()
createfood();
kx=mky*gridw;
ky=mkx*gridw;
createk();
}
a2.innerHTML="mkx:"+mky+"   mky:"+mkx
a3.innerHTML=map[mkx][mky]
}
function keydown(){
var kx=0;
var key=event.keyCode;
//alert(key)
switch(key){
case 37:
//alert("左");
move(-1,0);
break;
case 38:
//alert("上");
move(0,-1);
break;
case 39:
//alert("右");
move(1,0);
break;
case 40:
//alert("下");
move(0,1);
break;
}
}
onload=init
document.onkeydown=keydown;
function dd(){
for(var i=0;i<alldiv.length;i++){
ii=i+1
alldiv[ii].style.left=parseInt(alldiv[i].style.left)+x;
alldiv[ii].style.top=parseInt(alldiv[i].style.top)+x;
//alert(alldiv[1].style.left)
}
}
</script>
</head>
<body>
<div id="a1" style="z-index:-1000">1</div>
<div id="a2">2</div>
<div id="a3">3</div>
<button onclick="cleardiv()">removeChild</button>
<button onclick="dd()">>></button>
</body>
</html>
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-10-1 17:38 , Processed in 0.194860 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表