1、最终实现效果

2、实现思路
- 1、通过监听up、down、enter键,对搜索结果进行样式变换。
- 2、通过一个变量来储存事件触发次数,再用这个变量对收索结果个数进行求余,进而定位active元素的位置。。。
3、废话少说,上代码
var key = 0;
function downUpKey() {
document.onkeydown = function(e) {
var theEvent = e || window.event;
var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
var _key = key % $(".prompt-box").find('ul li').length;
var index;
if (code == 40) {
$(".prompt-box").find('ul li').removeClass('active');
$(".prompt-box").find('ul li').eq(_key).addClass('active');
key++;
} else if (code == 38) {
index = $(".prompt-box").find('ul li').filter(".active").index();
$(".prompt-box").find('ul li').removeClass('active');
$(".prompt-box").find('ul li').eq(index - 1).addClass('active');
key--;
} else if (code == 13) {
$(".prompt-box").addClass('hidden');
return false;
}
}
}