【问题标题】:jQuery: how to capture keypress key using live()jQuery:如何使用 live() 捕获按键
【发布时间】:2009-07-22 15:06:43
【问题描述】:

我需要在一些动态输入上捕获一个 tab keypress 事件,但是使用 keypress 事件的正常语法似乎没有捕捉到键码。

$('input').live('keypress', function (e) {
   if ( e.which == 9 )
       alert( 'Tab pressed' );
});

无论我按哪个键,当我通过 firebug 中的调试器时,这似乎都会捕获 0 作为按键。

【问题讨论】:

    标签: jquery live keypress


    【解决方案1】:

    尝试使用 .keyCode 而不是 .which:

    $('input').live('keypress', function (e) {
       if ( e.keyCode == 9 ){
           alert( 'Tab pressed' );
        }
    });
    

    似乎工作;)

    【讨论】:

      【解决方案2】:

      尝试收听keyupkeydown 而不是keypress (per this SO post)

      【讨论】:

      • 确实如此。 quirksmode 还包含有关事件的浏览器兼容性的信息,以及具有某些键的特定浏览器怪癖。我在我的代码中选择了keydown
      猜你喜欢
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      • 2010-11-21
      • 1970-01-01
      相关资源
      最近更新 更多