JavaScript implementation using the arrow keys to control the cursor in table cell switch _javascript tips

Source: Internet
Author: User
Screenshot of Effect:

HTML code:
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> Select shelf Number </title>
<script type= "Text/javascript" src= "01.js" ></script>
<style type= "Text/css" >
#table1 TD {
Text-align:center;
width:15%;
}
#table2 TD {
Text-align:center;
width:15%;
Background-color: #dcdcdc;
Cursor:pointer;
}
</style>
<body>
<table id= "table1" border= "0" cellpadding= "1" cellspacing= "1" style= "width:100%" >
<tr>
<td> Shelves </td>
<td> One layer </td>
<td> Two floor </td>
<td> Three Floor </td>
<td> Four Floor </td>
<td> Five Floor </td>
</tr>
</table>
<table id= "table2" border= "0" cellpadding= "1" cellspacing= "1" style= "width:100%"; >
<tr>
&LT;TD style= "Background-color: #ffffff;" >A001</td>
&LT;TD id= "td0" title= "a001-1|11" style= "Background-color": #6699FF; ></td>
&LT;TD id= "TD1" title= "a001-2|12" ></td>
&LT;TD id= "TD2" title= "a001-3|13" ></td>
&LT;TD id= "td3" title= "a001-4|14" ></td>
&LT;TD id= "td4" title= "a001-5|15" ></td>
</tr>
<tr>
&LT;TD style= "Background-color: #ffffff;" >A002</td>
&LT;TD id= "TD5" title= "a002-1|21" ></td>
&LT;TD id= "Td6" title= "a002-2|22" ></td>
&LT;TD id= "Td7" title= "a002-3|23" ></td>
&LT;TD id= "Td8" title= "a002-4|24" ></td>
&LT;TD id= "td9" title= "a002-5|25" ></td>
</tr>
<tr>
&LT;TD style= "Background-color: #ffffff;" >A003</td>
&LT;TD id= "Td10" title= "a003-1|31" ></td>
&LT;TD id= "Td11" title= "a003-2|32" ></td>
&LT;TD id= "td12" title= "a003-3|33" ></td>
&LT;TD id= "Td13" title= "a003-4|34" ></td>
&LT;TD id= "Td14" title= "a003-5|35" ></td>
</tr>
<tr>
&LT;TD style= "Background-color: #ffffff;" >A004</td>
&LT;TD id= "td15" title= "a004-1|41" ></td>
&LT;TD id= "Td16" title= "a004-2|42" ></td>
&LT;TD id= "Td17" title= "a004-3|43" ></td>
&LT;TD id= "Td18" title= "a004-4|44" ></td>
&LT;TD id= "Td19" title= "a004-5|45" ></td>
</tr>
<tr>
&LT;TD style= "Background-color: #ffffff;" >A005</td>
&LT;TD id= "TD20" title= "a005-1|51" ></td>
&LT;TD id= "Td21" title= "a005-2|52" ></td>
&LT;TD id= "Td22" title= "a005-3|53" ></td>
&LT;TD id= "Td23" title= "a005-4|54" ></td>
&LT;TD id= "td24" title= "a005-5|55" ></td>
</tr>
</table>
</body>

JavaScript code:
Copy Code code as follows:

var tdnum = 0;
var Trid = "TD";
Keyboard events
Document.onkeydown = function (event) {
Compatible Mozilla Firefox
if (Null = event) {
event = window.event;
}
if (Event.keycode = = 13) {
P13key ();
}
else if (event.keycode <= && event.keycode >= 37) {
KEYTD (Event.keycode);
}
}
Press the ENTER key
function P13key () {
var tdid = Trid + tdnum;
var tdtitle = document.getElementById (Tdid). getattribute ("title");
var pos = tdtitle.indexof ("|");
var seatname = tdtitle.substring (0, POS);
var Seatid = tdtitle.substring (pos + 1, tdtitle.length);
Window.alert (Seatname + "," + Seatid);
}
Transform color
function SetColor (OLDTD, NEWTD) {
document.getElementById (OLDTD). style.backgroundcolor= "#dcdcdc";
document.getElementById (NEWTD). style.backgroundcolor= "#6699FF";
}
Main code to implement switching function
function Keytd (key) {
Left
if (key = = 37) {
--tdnum;
if (null = = document.getElementById (Trid + tdnum)) {
tdnum++;
Return
}
SetColor (Trid + (Tdnum + 1), Trid + tdnum);
}
Right
else if (key = = 39) {
++tdnum;
if (null = = document.getElementById (Trid + tdnum)) {
tdnum--;
Return
}
SetColor (Trid + (tdnum-1), Trid + tdnum);
}
On
else if (key = = 38) {
Tdnum = tdnum-5;
if (null = = document.getElementById (Trid + tdnum)) {
Tdnum = Tdnum + 5;
Return
}
SetColor (Trid + (Tdnum + 5), Trid + tdnum);
}
Under
else if (key = = 40) {
Tdnum = Tdnum + 5;
if (null = = document.getElementById (Trid + tdnum)) {
Tdnum = tdnum-5;
Return
}
SetColor (Trid + (tdnum-5), Trid + tdnum);
}
}
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.