js获取键盘(上下左右)方向键,执行相应操作 .

JS获取键盘方向键,然后执行相应的响应事件。以一个简单的图片的左右切换为例,代码如下

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<mce:style type="text/css"><!--
.list {
 font-size:20px;
 text-align:center;
 line-height:40px;
}
--></mce:style><style type="text/css" mce_bogus="1">.list {
 font-size:20px;
 text-align:center;
 line-height:40px;
}</style>

<mce:script type="text/javascript"><!--
var menuArray = [
 {info:"栏目一",url:"#"},
 {info:"栏目二",url:"#"},
 {info:"栏目三",url:"#"},
 {info:"栏目四",url:"#"}
];

var menuPos = 0;
var menuSize = 4;

function $(id){
 return document.getElementById(id);
}

function showMenu(){
 for(var i=0;i<menuSize;i++){
  $("menu"+i).innerHTML = menuArray[i].info;
  $("menu"+i).style.backgroundImage = "url(menu_focus0.gif)";
 }
}

function menuFocus(num){
 //$("menu"+menuPos).style.backgroundColor = "#CCCCCC";
 $("menu"+menuPos).style.backgroundImage = "url(menu_focus0.gif)";
 $("menu"+menuPos).style.color = "#000000";
 $("menu"+menuPos).style.fontSize = "20px";
 menuPos+=num;
 
 if(menuPos>menuArray.length-1) menuPos=0;
 else if(menuPos<0) menuPos=menuArray.length-1;

 //$("menu"+menuPos).style.backgroundColor = "#00CCFF";
 $("menu"+menuPos).style.backgroundImage = "url(menu_focus1.gif)";
 $("menu"+menuPos).style.color = "#FFFFFF";
 $("menu"+menuPos).style.fontSize = "30px";
}

function deSelect(){
 location.href = menuArray[menuPos].url;
}

function init(){
 showMenu();
 menuFocus(0);
}

document.onsystemevent = grabEvent;
document.onkeypress = grabEvent;
document.onirkeypress = grabEvent;
document.onkeyup = grabEvent;
function grabEvent(){
 var keycode = event.which||event.keyCode;
 switch(keycode){
  case 1:
  case 38:
  case 269: //up
   return 0;
   break;
  case 40:
  case 2:
  case 270: //down
   return 0;
   break;
  case 37:
  case 3:
  case 271: //left
   menuFocus(-1);
   return 0;
   break;
  case 39:
  case 4:
  case 272: //right
   menuFocus(1);
   return 0;
   break;
  case 13: //enter
   deSelect();
   return 0;
   break;
  case 339: //exit
  case 340: //back
   location.href = "../index.htm";
   return 0;
      break;
 }
}
// --></mce:script>
</head>

<body leftmargin="0" topmargin="0" οnlοad="init()">
<div style="position:absolute; left:0px; top:0px; width:640px; height:50px; line-height:50px; font-size:30px; color:#FFFFFF; text-align:center; background:#00CCFF;">菜单交互(背景交互)</div>

<div style="position:absolute; left:0px; top:100px; width:640px; height:40px;">
<table width="620" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="140" height="40" class="list" id="menu0"></td>
    <td width="20"></td>
    <td width="140" class="list" id="menu1"></td>
    <td width="20"></td>
    <td width="140" class="list" id="menu2"></td>
    <td width="20"></td>
    <td width="140" class="list" id="menu3"></td>
  </tr>
</table>
</div>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤来实现JavaScript利用键盘方向键(上下键)控制表格行选中: 1. 首先,为表格的每一行添加一个事件监听器,监听键盘方向键的按下事件。 2. 当方向键按下时,获取当前选中的行的索引,以及表格总共有多少行。 3. 根据按下的方向键,更新选中行的索引,如果已经到达了表格的边缘,则循环到另一端。 4. 根据更新后的选中行的索引,添加或移除行的选中状态。 以下是一个简单的实现示例: ```html <table> <tr> <td>Row 1</td> </tr> <tr> <td>Row 2</td> </tr> <tr> <td>Row 3</td> </tr> </table> <script> const table = document.querySelector('table'); const rows = table.querySelectorAll('tr'); let selectedRow = 0; // 为每一行添加事件监听器 rows.forEach((row, index) => { row.addEventListener('keydown', (event) => { if (event.key === 'ArrowUp') { // 上方向键 selectedRow = selectedRow === 0 ? rows.length - 1 : selectedRow - 1; } else if (event.key === 'ArrowDown') { // 下方向键 selectedRow = selectedRow === rows.length - 1 ? 0 : selectedRow + 1; } // 更新行的选中状态 rows.forEach((row, index) => { if (index === selectedRow) { row.classList.add('selected'); } else { row.classList.remove('selected'); } }); }); }); // 默认选中第一行 rows[selectedRow].classList.add('selected'); </script> <style> .selected { background-color: yellow; } </style> ``` 在上面的示例中,我们为每一行添加了一个 `keydown` 事件监听器,当方向键按下时,更新了选中行的索引,并根据索引添加或移除行的选中状态。同时,我们还为选中行添加了一个 `.selected` 样式,以便用户能够清楚地看到哪一行被选中了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值