我读过这里这个答案指南上可聚焦元素写arrowkey导航? 但它不是像我这样的新手,了解详细说明不够。
我需要这样的东西行,我有div的4x4网格在我的DOM。 我希望能够走动一个选择,然后可以选择与进入集中股利。
编辑:粘贴一些代码,
这是我尝试当我意识到我是总错误的轨道上:
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]];
但林坚持什么那么..
这里有一个办法做到这一点使用普通的JavaScript。 代码的第一部分是一些跨浏览器的功能来添加的事件处理程序和添加/删除的类。 你会注意到,我添加/删除类,以显示活动项目,而不是直接添加/删除风格。 这使得该风格在CSS中,而不是通常是一个好主意的JavaScript。
工作演示: 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;
}
});
})();
我已经使用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
}