我需要在一些动态输入上捕获选项卡按键事件,但使用按键事件的正常语法似乎没有捕捉到关键代码。
$('input').live('keypress', function (e) {
if ( e.which == 9 )
alert( 'Tab pressed' );
});
无论我按哪个键,当我通过 firebug 中的调试器时,这似乎都将 0 捕获为按键。
尝试使用 .keyCode 而不是 .which:
$('input').live('keypress', function (e) {
if ( e.keyCode == 9 ){
alert( 'Tab pressed' );
}
});
似乎工作;)
尝试听keyup
或keydown
代替keypress
(根据此 SO 帖子)