高亮显示表格的行,并支持上、下箭头键移动
一个表格,如何高亮显示表格的其中一行,并支持上、下箭头键移动,使行高亮显示,如按一下下箭头,高亮条向下一行移动,按一下上箭头则向上一行移动。
------解决方案--------------------大概意思一下
<body onKeyDown= "kd() ">
<script type= "text/javascript ">
function kd()
{
var obj = document.getElementById( "table ").childNodes[0].childNodes;
var key = event.keyCode;
var index;
for (var i=0; i <obj.length; i++)
{
if (obj[i].style.background == "#00ffff ")
{
index = i;
}
}
if ((key == 38) && (index> 0))
{
obj[index].style.background = "#ffffff ";
obj[index-1].style.background = "#00ffff ";
}
if ((key == 40) && (index <obj.length-1))
{
obj[index].style.background = "#ffffff ";
obj[index+1].style.background = "#00ffff ";
}
}
</script>
<table id= "table " border= "1 ">
<tbody>
<tr style= "background:#00FFFF ">
<td> 1 </td>
<td> </td>
</tr>
<tr>
<td> 2 </td>
<td> </td>
</tr>
<tr>
<td> 3 </td>
<td> </td>
</tr>
<tr>
<td> 4 </td>
<td> </td>
</tr>
</tbody>
</table>
</body>