|
<!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> |
|