在jsfiddle 链接中测试的完整工作代码片段
HTML
<ul>
<li><a href="#">First Link</a></li>
<li> <a href="#">Second Link</a></li>
<li> <a href="#">Third Link</a></li>
</ul>
CSS
li.selected {background:yellow}
a:hover{color:blue}
a {
color:inherit;
text-decoration: none;
}
ul
{
list-style-type: none;
}
JS
var li = $('li');
var liSelected;
$(window).keydown(function(e){
if(e.which === 40){
if(liSelected){
liSelected.removeClass('selected');
next = liSelected.next();
if(next.length > 0){
liSelected = next.addClass('selected');
}else{
liSelected = li.eq(0).addClass('selected');
}
}else{
liSelected = li.eq(0).addClass('selected');
}
}else if(e.which === 38){
if(liSelected){
liSelected.removeClass('selected');
next = liSelected.prev();
if(next.length > 0){
liSelected = next.addClass('selected');
}else{
liSelected = li.last().addClass('selected');
}
}else{
liSelected = li.last().addClass('selected');
}
}
});