糯米文學吧

位置:首頁 > 計算機 > 計算機硬件

利用JS來控制鍵盤的上下左右鍵

主要介紹了利用JS來控制鍵盤的'上下左右鍵示例代碼。需要的朋友可以過來參考下,希望對大家有所幫助

利用JS來控制鍵盤的上下左右鍵

這是一個JS初級代碼,想學JS的朋友,可以研究下或者擴展下,最好能用JS實現整個鍵盤的控制,那感覺就很有意思了。

  具體代碼如下:

代碼如下:

<style>

light{background:#08246B;color:white;}

</style>

<table border="1" width="70%" id="ice">

<tr>

<td><input type='text'></td>

<td><input type='text'></td>

<td><input type='text'></td>

<td><input type='text'></td>

</tr>

<tr>

<td><input type='text'></td>

<td><input type='text'></td>

<td><input type='text'></td>

<td><input type='text'></td>

</tr>

<tr>

<td><input type='text'></td>

<td><input type='text'></td>

<td><input type='text'></td>

<td><input type='text'></td>

</tr>

<tr>

<td><input type='text'></td>

<td><input type='text'></td>

<td><input type='text'></td>

<td><input type='text'></td>

</tr>

<tr>

<td><input type='text'></td>

<td><input type='text'></td>

<td><input type='text'></td>

<td><input type='text'></td>

</tr>

</table>

<script language="javascript">

<!--

//定義初始化行列

var currentLine=-1;

var currentCol=-1;

ydown=function(e){

e=t||e;

switch(ode){

case 37: //左鍵

currentCol--;

changeItem();

break;

case 38: //向上鍵

currentLine--;

changeItem();

break;

case 39: //右鍵

currentCol++;

changeItem();

break;

case 40: //向下鍵

currentLine++;

changeItem();

break;

default:

break;

}

}

//方向鍵調用

function changeItem(){

if()

var it=lementByIdx_x("ice")dren[0];

else

var it=lementByIdx_x("ice");

for(i=0;i<th;i++){

[i]sName="";

}

if(currentLine<0){

currentLine=th-1;

}

if(currentLine==th){

currentLine=0;

}

var objtab=;

var objrow=[currentLine]lementsByTagName_r("INPUT");

if(currentCol<0){

currentCol=th-1;

}else if(currentCol==th){

currentCol=0;

}

objrow[currentCol]ct();

//調試使用

[currentLine]sName="highlight";

}

//-->

</script>