JS up and down key control focus

Source: Internet
Author: User

  This article is mainly about JS key control around the focus of the sample code for a detailed introduction, the need for friends can come to the reference, I hope to help you.

As follows: The following code://begin---------------up and down key control   if (' ${iscontrol_mchntid} '. IndexOf (' ${mchntid} ')!=-1) {  var texts = new Array (); Location var x = 2 set to focus; var y = 3; var maxx = 0; var maxy = 0; Window.onload=function () {var inputs = $ ("[location]"); for (var i = 0; i < inputs.length; i++) {Texts.push (inputs[i)); for (var i = 0; i < texts.length i++) {texts[i].onfocus = new Function ("setcurrent (" + Texts[i].getattribute ("Locati On ") +" ') "); var crtx = parseint (Texts[i].getattribute ("Location"). Split (",") [0]); var crty = parseint (Texts[i].getattribute ("Location"). Split (",") [1]); Maxx = Maxx < Crtx? Crtx:maxx; Maxy = Maxy < Crty? Crty:maxy; Texts[i].onkeydown = function (e) {e = e | | window.event switch (e.keycode) {case 38:setposition (x,y,38); break;//on Case 40 : SetPosition (x,y,40), break;//case 37:setposition (x,y,37), break;//left cases 39:setposition (x,y,39), break;//right 45: SetPosition (x,y,45); Insert key/Return key in the input box is deleted and the input library has a value is deleted other to return to the previous page Default:reTurn true; }}; }}; function SetPosition (x,y,keycode) { //Here Add the dynamic change position logic----begin //up and down, only change y coordinate, x coordinate automatically change/around, only change x coordinate, y coordinate automatically change if (keycode = = ' && x = = ' 3 ') {if (y== ' 3 ') | | y== ' 4 ' | | y== ' 5 ' | | y== ' 6 ' | | y== ' 7 ' | | y== ' 8 ') {y= ' 3 ';}} if (keycode = = ' && x = = ' 4 ') {if (y== ' 3 ') | | y== ' 4 ' | | y== ' 5 ' | | y== ' 6 ' | | y== ' 7 ' | | y== ' 8 ') {y= ' 3 ';}} Add the dynamic change position logic----End   if (keycode = = ' = ') {x =--x; } if (keycode = = ') {x = ++x; } if (keycode = ' 3 7 ') {y =--y; } if (keycode = = ' ") {y = ++y; } moveposition (X,y,keycode); function Moveposition (x1,y1,keycode) {if (keycode = = ' 45 ') {//the cursor is on the object of input var st = x1+ "," +Y1; if ($ ("input[location=" +st+ "']"). attr ("type") = = "text") {var oldval = $ ("input[location= '" +st+ "']"). Val (); var newval = oldval.substring (0, OLDVAL.LENGTH-1); $ ("input[location= '" +st+ "]"). Val (newval); return false; }else{history.go ( -1);  return false;} X1 = x1 > Maxx? 1:x1; y1 = y1 > Maxy? 1:y1; X1 = X1 < 1? maxx:x1; Y1 = y1 < 1? Maxy:y1;   var j = 0; for (; J < Texts.length; J + +) {if (Texts[j].getattribute ("location") = = x1 + "," + y1) {  texts[j].focus (); }  if (j = = texts.length) {switch (keycode) {case 38:moveposition (--x1,y1,keycode); break;//on case 40:moveposition (+ +x1,y1,keycode); break;//case 37:moveposition (x1,--y1,keycode); break;//left Cases 39:moveposition (X1,++y1,keycode); break;//Right}}}  function setcurrent (location) {x = Location.split (",") [0]; y = Location.split (",") [1];}} End---------------up and down key control    
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.