onkeydown函数应用

直接在js实现

var div=document.createElement(‘div’);
document.body.appendChild(div);
div.style.width=“100px”;
div.style.height=“100px”;
div.style.backgroundColor=“red”;
div.style.position=“absolute”;
div.style.top=“20px”;
div.style.left=“20px”;

document.οnkeydοwn=function(e){
switch(e.which){
case 38:
div.style.top=parseInt(div.style.top)-5+“px”;break;
case 40:
div.style.top=parseInt(div.style.top)+5+“px”;break;
case 39:
div.style.left=parseInt(div.style.left)+5+“px”;break;
case 37:
div.style.left=parseInt(div.style.left)-5+“px”;break;
}

 }

你可能感兴趣的:(onkeydown函数应用)