在DIV元素上捕获按键(或keydown)事件 [英] Capture key press (or keydown) event on DIV element

查看:622
本文介绍了在DIV元素上捕获按键(或keydown)事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何让DIV元素获得焦点?

解决方案

(1)设置 tabindex 属性:

 < div id =mydivtabindex =1/> 

(2)绑定到keydown:



<$ ($'code> $('#mydiv')。bind('keydown',function(event){
//console.log(event.keyCode);
switch( event.keyCode){
// ...您的按键操作.....
}
});

设定开始时的焦点:

  $(function(){
$('#mydiv')。focus();
});

删除 - 如果您不喜欢 - div focus border,设置 outline:none 在CSS中。



请参阅 keycodes 更多 keyCode 可能性。



假设您使用jQuery的所有代码。




How do you trap the keypress or key down event on a DIV element (using jQuery)?

What is required to give the DIV element focus?

解决方案

(1) Set the tabindex attribute:

<div id="mydiv" tabindex="1" />

(2) Bind to keydown:

 $('#mydiv').bind('keydown', function(event) {
    //console.log(event.keyCode);
    switch(event.keyCode){
       //....your actions for the keys .....
    }
 });

To set the focus on start:

$(function() {
   $('#mydiv').focus();
});

To remove - if you don't like it - the div focus border, set outline: none in the CSS.

See the table of keycodes for more keyCode possibilities.

All of the code assuming you use jQuery.

#

这篇关于在DIV元素上捕获按键(或keydown)事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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