我有一个部分的数组。使用向上和向下箭头键,我想获得当前值和下一个值,并且不循环,如下所示。jQuery - 向上和向下键从数组中获取值
var sections = [ "A", "B","C","D"];
$(document).keydown(function(e){
var k= e.which;
if(k==40){ sections.push(sections.shift()); }
else if(k==38){ sections.unshift(sections.pop()); }
$('body').html('You just went to ' + sections[0]); // current section.
// How to return the previous section that I was in?
// For example: You just left section A then went to section B
// Do not loop, return: You just reached end/beginning
});
工作小提琴:http://jsfiddle.net/9977ov2x/
所以你的代码工作,你认为它需要审查?如果是的话,张贴在http://codereview.stackexchange.com/ – undefined 2014-10-31 03:44:42
@Vohuman不,检查第二和第三//评论 – 2014-10-31 03:46:38