这篇文章主要介绍了javascript上下方向键控制表格行选中并高亮显示的方法,涉及javascript针对键盘按键操作相应的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了javascript上下方向键控制表格行选中并高亮显示的方法。分享给大家供大家参考。具体实现方法如下:
<style> tr.highlight { background:#08246B; color:white; } </style> <table> <tr> <td>123</td> <td>234</td> <td>abc</td> <td>def</td> </tr> <tr> <td>123</td> <td>234</td> <td>abc</td> <td>def</td> </tr> <tr> <td>123</td> <td>234</td> <td>abc</td> <td>def</td> </tr> <tr> <td>123</td> <td>234</td> <td>abc</td> <td>def</td> </tr> <tr> <td>123</td> <td>234</td> <td>abc</td> <td>def</td> </tr> </table> <script language="javascript"> <!-- var currentLine = -1; document.onkeydown = function(e) { e = window.event || e; switch(e.keyCode) { case 38: currentLine--; changeItem(); break; case 40: currentLine++; changeItem(); break; default: break; } } function selectTR() { currentLine=window.event.srcElement.parentElement.rowIndex; //alert(currentLine); changeItem(); } //改变选择项目 function changeItem() { if(document.all) var it = document.getElementById("ice").children[0]; else var it = document.getElementById("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; it.rows[currentLine].className = "highlight"; } //--> </script>
希望本文所述对大家的javascript程序设计有所帮助。
您可能感兴趣的文章: