简体   繁体   中英

Using arrow keys to navigate in a table

UPDATE

I see that the code works in FF but not chrome, so the code itself works. Could it be to this part in the body?:

<body onkeypress="return onKeyPress(event)" onload="javascript:initBoard('Container'); makeBoard()">

What should I change to let Chrome pick up the key events too?

END UPDATE


I have a table I want to navigate in it using the arrow keys. Problem is, it aint working. My javascript code:

function setSel(x, y) {
    // Wrap around off the edges of the board
    if (x > 8) x = 0;
    if (x < 0) x = 8;
    if (y > 8) y = 0;
    if (y < 0) y = 8;

    var e = document.getElementById("Cell" + x + y);
    if (!e)
        return;
    if (sel)
        delClass(sel, "Selected");
    sel = e;
    addClass(sel, "Selected");
}

function onKeyPress(e) {
    if (sel == null)
        return;

    var keynum;
    var keychar;
    var numcheck;

    var e = window.event || e
    keynum = e.keyCode == 0 ? e.which : e.keyCode;

    switch(keynum) {
    case 37: // left arrow
        setSel(sel.x - 1, sel.y);
        return false;
    case 38: // up arrow
        setSel(sel.x, sel.y - 1);
        return false;
    case 39: // right arrow
        setSel(sel.x + 1, sel.y);
        return false;
    case 40: // down arrow
        setSel(sel.x, sel.y + 1);
        return false;
    case 46: // backspace/delete/space
    case 32:
    case 8: 
        board.setValue(sel.x, sel.y, 0);
        board.render();
        return false;
    default:
        keychar = String.fromCharCode(keynum);
        if ((/\d/).test(keychar)) {
            var i = parseInt(keychar);
            if (i >= 0 && i <= 9) {
                board.setValue(sel.x, sel.y, parseInt(keychar));                
                board.render();
                return false;           
            }
        }
    }

    board.render();
    return true;
}

I've found this fiddle: http://jsfiddle.net/BdVB9/ which does exactly what I need, but I am unsuccessful replicating it with my own code. So what am I missing?

经过更多研究后,Chromium似乎不支持箭头键: https//code.google.com/p/chromium/issues/detail? id = 2606http://help.dottoro.com/ljlwfxum。 PHP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM