利用JS来控制键盘的上下左右键(示例代码)
2013-12-14 00:00
876 查看
这是一个JS初级代码,想学JS的朋友,可以研究下或者扩展下,最好能用JS实现整个键盘的控制,那感觉就很有意思了。
具体代码如下:
具体代码如下:
<style> tr.highlight{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; document.onkeydown=function(e){ e=window.event||e; switch(e.keyCode){ 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(document.all) var it=document.getElementByIdx_x("ice").children[0]; else var it=document.getElementByIdx_x("ice"); for(i=0;i<it.rows.length;i++){ it.rows[i].className=""; } if(currentLine<0){ currentLine=it.rows.length-1; } if(currentLine==it.rows.length){ currentLine=0; } var objtab=document.all.ice; var objrow=objtab.rows[currentLine].getElementsByTagName_r("INPUT"); if(currentCol<0){ currentCol=objrow.length-1; }else if(currentCol==objrow.length){ currentCol=0; } objrow[currentCol].select(); //调试使用 it.rows[currentLine].className="highlight"; } //--> </script>
相关文章推荐
- 利用JS来控制键盘的上下左右键(示例代码)
- 利用JS来控制键盘的上下左右键(示例代码)
- js 上下左右键控制焦点(示例代码)
- js 上下左右键控制焦点(示例代码)
- JS控制文本框内键盘上下左右键的焦点
- JS实现用键盘控制DIV上下左右+放大缩小与变色
- easyui datagrid 键盘上下控制选中行示例
- js 键盘控制表格input上下左右移动
- js键盘上下左右键怎么触发function(实例讲解)
- Js判断H5上下滑动方向及滑动到顶部和底部判断的示例代码
- JS控制图片翻转示例代码(兼容firefox,ie,chrome)
- js监听键盘事件示例代码
- js 绑定键盘鼠标事件示例代码
- js和jquery实现监听键盘事件示例代码
- 键盘上下键移动选择table表格行的js代码
- 利用three.js画一个3D立体的正方体示例代码
- JS控制图片等比例缩放的示例代码
- js控制图片的缩放代码示例
- js实现用键盘控制DIV上下左右+放大缩小与变色