首页 > JavaScript,我的 > JS获取键盘值,撸了个实例

JS获取键盘值,撸了个实例

JS获取键盘值,撸了个实例

<!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=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
#abc{ width:50px; height:50px; background-color:#000000; position:relative;}

</style>


</head>
<body onkeyup="keyUp();">
    <div id="abc" style="left:400px; top:300px; " ></div>


    <script language="javascript">
 function keyUp(){
            if(navigator.appName == "Microsoft Internet Explorer"){
            var keycode = event.keyCode;               
             }else{
             var keycode =  keyUp.caller.arguments[0].which;        
            }
           
 a = document.getElementById("abc").style.top;

 a = a.substr(0,a.length-2);

  b = document.getElementById("abc").style.left;

 b = b.substr(0,b.length-2); 

if (keycode == 38)
{

document.getElementById("abc").style.top=a-10+"px";


}

if (keycode == 40)
{
a=a-0+10;
document.getElementById("abc").style.top=a+"px";


}

if (keycode == 37)
{
document.getElementById("abc").style.left=b-10+"px";

}

if (keycode == 39)
{
b=b-0+10
document.getElementById("abc").style.left=b+"px";

}

     }
</script>
</body>



</html>

上一篇: preg_match_all

下一篇: fsck died with exit status 4