键盘向上和向下箭头 [英] Keyboard up and down arrows

查看:313
本文介绍了键盘向上和向下箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自动完成搜索,在键入几个字符,它会显示所有的名字,输入的字符相匹配。我使用DIV标签在JSP填充这个数据,通过使用鼠标,我能够选择的名称。但我想在DIV标签,选择名称使用键盘上下箭头来选择。任何人都可以请帮我从这。

I have one autocomplete search, in which by typing few characters it will show all the names, which matches the entered character. I am populating this data in the jsp using DIV tag, by using mouse I'm able to select the names. But I want to select the names in the DIV tag to be selected using the keyboard up and down arrow. Can anyone please help me out from this.

推荐答案

使用的onkeydown 的onkeyup 事件检查重点preSS事件结果的div:

Use the onkeydown and onkeyup events to check for key press events in your results div:

var UP = 38;
var DOWN = 40;
var ENTER = 13;

var getKey = function(e) {
  if(window.event) { return e.keyCode; }  // IE
  else if(e.which) { return e.which; }    // Netscape/Firefox/Opera
};


var keynum = getKey(e);

if(keynum === UP) {
  //Move selection up
}

if(keynum === DOWN) {
  //Move selection down
}

if(keynum === ENTER) {
  //Act on current selection
}

这篇关于键盘向上和向下箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆