JavaScript keyboard up and down keys operation (selection) _javascript tips

Source: Internet
Author: User
Two paragraphs using the keyboard's upper and lower keys to select the code:
First Paragraph
<script language= "JScript" > Function Document.onkeydown () {var rowsarray = document.all (' otable ' ). Rows; for (Var i=0;i<rowsarray.length;i++) {if (rowsarray[i].children[0].style.backgroundcolor== ' #dcdcdc ') { Switch (window.event.keyCode) {case 38:if (i-1>=0) { Rowsarray[i-1].children[0].style.backgroundcolor = ' #dcdcdc '; Rowsarray[i].children[0].style.backgroundcolor = '; } break; Case 40:if (i+1<=rowsarray.length-1) {Rowsarray[i+1].children [0].style.backgroundcolor = ' #dcdcdc '; Rowsarray[i].children[0].style.backgroundcolor = '; } break; } break; }} function Document.onclick () {if (window.event.srcElement.tagNa me!= ' TD ') {return;} var Rowsarray = document.all (' otable '). Rows; for (Var i=0;i<rowsarray.length;i++) {if (rowsarray[i].cells[0]==window.event.srcelement) { Rowsarray[i].cells[0].style.backgroundcolor = ' #dcdcdc '; }else{rowsarray[i].cells[0].style.backgroundcolor = '; }} </Script> <style> TD {cursor:hand} </style> <table id= " Otable "> <tr> <td > Mouse click or press ↑↓ to change line's color </td> </tr> <tr> <td> mouse click or press ↑↓ to change the line color </td> </tr> <tr> <td> Mouse click or press ↑↓ to change the line color &LT;/TD&G T </tr> <tr> <td> Mouse clicks or press ↑↓ to change the color of the line </td> </tr> </table>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

second paragraph
<table id= "TB" > <tr><td>00000000000000</td> </tr> <tr><td>111111111111 11</td> </tr> <tr><td>22222222222222</td></tr> <tr><td>3333333333 3333</td></tr> </table> <script> var i=0; function Document.onkeydown () {if (Event.keycode = =) {for (Var K=0;k<document.getelementbyid (' TB '). rows.length;k++) {document.getElementById (' TB '). Rows (k). bgcolor= ""; } document.getElementById (' TB '). Rows (--i%tb.rows.length). bgcolor= "#FF0000"; Document.all.show.value=document.getelementbyid (' TB '). Rows (i%tb.rows.length). innertext; } if (event.keycode==) {for (Var K=0;k<document.getelementbyid (' TB '). rows.length;k++) { document.getElementById (' TB '). Rows (k). bgcolor= ""; } document.getElementById (' TB '). Rows (++i%document.getelementbyid (' TB '). Rows.length). bgcolor= "#FF0000"; Document.all.show.value=document.getelementbyid (' TB '). Rows (i%tb.rows.length). innertext; } </script> <input type= ' text ' name= ' show ' value= ' do ' >
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.