簡體   English   中英

使用箭頭鍵在表格中導航

[英]Using arrow keys to navigate in a table

UPDATE

我看到代碼可以在FF中運行,但不能在chrome中運行,因此代碼本身可以運行。 可能是身體的這一部分?

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

我應該怎么做才能讓Chrome也接管關鍵事件?

結束更新


我有一個要使用箭頭鍵瀏覽的表。 問題是,它不能正常工作。 我的JavaScript代碼:

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;
}

我發現了這個小提琴: http : //jsfiddle.net/BdVB9/確實可以滿足我的需要,但是我用自己的代碼將其復制失敗。 那我想念什么呢?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM