希望用鼠标和键盘选中表格的研究

本文介绍了一个使用JavaScript实现的网页表格中TD元素的鼠标和键盘控制案例。通过为表格单元格添加点击和键盘事件,实现了单元格高亮显示的功能。但键盘事件触发时未能正确获取到目标TD元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<html>
<body>
<table bgcolor="#FFFFFF">
  <tr>
      <td id="td1">  click me!          </td>
      <td id="td2">*********************</td>
      <td id="td3">  click me too!      </td>
  </tr> 
  <tr>
      <td id="td4">  aaaaa  </td>
  </tr>
</table>
</body>
</html>

<script   language="JavaScript">
var count;
var selectObject = document.getElementsByTagName("td");
alert(selectObject.length);
for(var i=0;i<selectObject.length;i++) {

selectObject [i].onclick=doClick;
count = i;
selectObject [i].onkeydown=doKeyDown;

//selectObject.onclick=doClick;
var oldObj;
var obj;
var nextObj;

function doClick() {
obj = event.srcElement;
alert(obj);
if(typeof oldObj != "object") {
oldObj = obj;
} else {
  clearStyle(oldObj);
  }
  addStyle(obj);
}

function clearStyle(oldObj) {
  oldObj.style.borderLeft      =   ""; 
  oldObj.style.borderRight     =   ""; 
  oldObj.style.borderTop       =   ""; 
  oldObj.style.borderBottom    =   ""; 
  oldObj.style.padding         =   ""; 
  oldObj.style.backgroundColor =   "";
  oldObj   =   obj; 
}

function addStyle(obj) {
  alert("a");
  obj.style.borderLeft      =   "1px   solid   buttonhighlight"; 
  obj.style.borderRight     =   "1px   solid   buttonshadow"; 
  obj.style.borderTop       =   "1px   solid   buttonhighlight"; 
  obj.style.borderBottom    =   "1px   solid   buttonshadow"; 
  obj.style.padding         =   "1px"; 
  obj.style.backgroundColor =   "#cccccc"; 
  oldObj = obj;
}
function addNextStyle(nextObj) {
 nextObj.style.borderLeft      =   "1px   solid   buttonhighlight"; 
 nextObj.style.borderRight     =   "1px   solid   buttonshadow"; 
 nextObj.style.borderTop       =   "1px   solid   buttonhighlight"; 
 nextObj.style.borderBottom    =   "1px   solid   buttonshadow"; 
 nextObj.style.padding         =   "1px"; 
 nextObj.style.backgroundColor =   "#cccccc"; 
 oldObj = nextObj;
}
function doKeyDown() {
 alert("count="+count);
 alert("window.event=" + window.event);
 var a = window.event.keyCode;
 if(a == 39) {
  alert(selectObject [count+1]);
  nextObj = selectObject [count];
  alert (nextObj);
  clearStyle(oldObj);
  addNextStyle(nextObj);
 }
}

</script>

 

希望用鼠标和键盘控制TD,结果鼠标成功了,键盘取不到触发事件的对象,备忘一下.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值