The keydown
event is sent to an element when the user first presses a key on the keyboard. It can be attached to any element, but the event is only sent to the element that has the focus. Focusable elements can vary between browsers, but form elements can always get focus so are reasonable candidates for this event type.jquery
以上是从jquery文档中copy下的一段文本,它说明咱们能够给form元素绑定keydown事件,由于它们能够获取焦点,但怎样去给div、span绑定呢?
答案就是tabindex这个属性
js中改变这个属性:jsObj.tabIndex
jquery : $(selector).attr("tabindex",value)网络
元素的tabindex属性用来定义元素是否能够获取焦点,是否能够经过连续的焦点导航(通常状况是按tab键)获取焦点,与你获取焦点的顺序。
其值必须是整数值。
若是没有设置,或者设置的值不正确,则按照惯例执行。
若是是负数,用户不能够经过连续的焦点导航获取焦点,但能够用其余方式获取焦点。
若是是零,则能够经过连续的焦点导航获取焦点,按照惯例肯定顺序。
若是是正数,则能够经过连续的焦点导航获取焦点,按照该值肯定顺序。this
div默认是得不到焦点的,能够为其设置tabindex属性使其能够得到焦点。也就能够绑定键盘事件。spa
事例 :code
<span id="myspan"></span>orm
js:事件
$("#myspan").attr("tabindex",0);element
$("#myspan").focus();文档
此文章来自网络转载