0

我已在此处阅读此答案Guide for writing arrowkey navigation on focusable elements?但对于像我这样的新手来说,它还不够详细。

我需要这样的东西,我的 DOM 中有一个带有 div 的 4x4 网格。我希望能够在选择器周围移动,然后能够通过 enter 选择焦点 div。

编辑:粘贴一些代码,

当我意识到自己完全走错了路时,这就是我正在尝试的:

function arrowKeys(input){
    var keyCode = input.keyCode;
    var border = document.getElementById(keyid);
    var removeBorder = border.removeAttribute("style");

    if(keyCode == 38 || keyCode == 39 || keyCode == 40 || keyCode == 37){
        var addborder = border.setAttribute("style", "border:2px solid red");
        border.addborder;
        if(keyCode == 38){
            removeBorder;
            keyid = keyid - 4;
            border.addborder;
            console.log(keyid + " upp");
        }
        if (keyCode == 39){
            removeBorder;
            keyid++;
            border.addborder;
            console.log(keyid + " right");
        }
        if(keyCode == 40){      // right arrowkey
            removeBorder;   
            keyid = keyid + 4;
            border.addborder;
            console.log(keyid + " down");
        }
        if (keyCode == 37){     // left arrowkey
            removeBorder;
            keyid--;
            border.addborder;
            console.log(keyid + " left");
        }
    }
}

我想你需要在这样的行上使用一个数组,

var navigationMap = [[1,2,3,4][5,6,7,8][9,10,11,12][13,14,15,16]];

但是我一直卡在那里..

4

2 回答 2

1

Here's a way to do this using plain javascript. The first part of the code is some cross browser functions to add event handlers and add/remove classes. You'll notice that I add/remove a class to show the active item rather than directly adding/removing a style. This puts the style in the CSS rather than in the javascript which is generally a good idea.

Working demo: http://jsfiddle.net/jfriend00/yMMxX/

(function() {
    // refined add event cross browser
    function addEvent(elem, event, fn) {
        if (typeof elem === "string") {
            elem = document.getElementById(elem);
        }

        // avoid memory overhead of new anonymous functions for every event handler that's installed
        // by using local functions
        function listenHandler(e) {
            var ret = fn.apply(this, arguments);
            if (ret === false) {
                e.stopPropagation();
                e.preventDefault();
            }
            return(ret);
        }

        function attachHandler() {
            // set the this pointer same as addEventListener when fn is called
            // and make sure the event is passed to the fn also so that works the same too
            var ret = fn.call(elem, window.event);   
            if (ret === false) {
                window.event.returnValue = false;
                window.event.cancelBubble = true;
            }
            return(ret);
        }

        if (elem.addEventListener) {
            elem.addEventListener(event, listenHandler, false);
        } else {
            elem.attachEvent("on" + event, attachHandler);
        }
    }



    function addClass(elem, cls) {
        var oldCls = elem.className;
        if (oldCls) {
            oldCls += " ";
        }
        elem.className = oldCls + cls;
    }

    function removeClass(elem, cls) {
        var str = " " + elem.className + " ";
        elem.className = str.replace(" " + cls + " ", " ").replace(/^\s+|\s+$/g, "");
    }


    function findItem(items, target) {
        for (var i = 0; i < items.length; i++) {
            if (items[i] === target) {
                return(i);
            }
        }
        return(-1);
    }

    var keys = {up: 38, down: 40, left: 37, right: 39};
    var cards = document.getElementById("game-board").getElementsByClassName("card");
    addEvent(document, "keydown", function(e) { 
        // get key press in cross browser way
        var code = e.which || e.keyCode;
        // number of items across
        var width = 4;
        var increment, index, newIndex, active;

        switch(code) {
            case keys.up:
                increment = -width;
                break;
            case keys.down:
                increment = width;
                break;
            case keys.left:
                increment = -1;
                break;
            case keys.right:
                increment = 1;
                break;
            default:
                increment = 0;
                break;
        }
        if (increment !== 0) {
            active = document.getElementById("game-board").getElementsByClassName("active")[0];
            index = findItem(cards, active);
            newIndex = index + increment;
            if (newIndex >= 0 && newIndex < cards.length) {
                removeClass(active, "active");
                addClass(cards[newIndex], "active");
            }
            // prevent default handling of up, down, left, right keys
            return false;
        }
    });
})();
于 2013-03-28T04:12:46.120 回答
0

我已经用 jQuery 做到了这一点。您可以使用以下方式连接到事件:

  $(".some-css-class-on-your-divs").keydown(function (e)
  {
    HandleKeyDown(e, $(this));
  })

然后用另一个 javascript 方法处理事件

function HandleKeyDown(event, jqNode)
{
  event = event || window.event;
  var charCode = event.charCode || event.keyCode;


  if (charCode == 40)
  { // down arrow key
      ///  do your thing here
  }
   // charcode 38 is up, and so on
}
于 2013-03-27T23:18:52.737 回答