【问题标题】:Binding keypress event using JQuery not working in Chrome使用 JQuery 绑定按键事件在 Chrome 中不起作用
【发布时间】:2010-08-05 18:32:33
【问题描述】:

我正在使用 Ubuntu 10 并在 Firefox 3.6 和 Chrome 5 中试用以下代码。

    $(document).ready(function(){
        $(document).bind("keypress", function(e){
            alert("Pressed");
        });
    });

令人惊讶的是,上面的代码在 FF3.6 中运行良好。即每按下一个键,我都会收到一个警告框。

但在 Chrome 中,我看到了不同的行为。例如,当我按下向上/向下翻页、向上/向下/向左'右键时,我没有收到警告框(不幸的是,这些是我非常想跟踪的关键事件)。

有谁知道为什么 Chrome 没有将“keypress”事件绑定到某些键,例如向上/向下、向左/向右箭头?

【问题讨论】:

    标签: jquery ubuntu google-chrome


    【解决方案1】:

    onkeypress 事件最初是 Internet Explorer 的专有事件,后来包含在其他浏览器中,但实现不同。 Chrome/Safari 忠实于微软的实现,the documentation 声明:

    从 Microsoft Internet Explorer 4.0 开始,onkeypress 事件会触发,并且可以为以下键取消:

    字母:A - Z(大写和小写)
    数字:0 - 9
    符号:! @ # $ % ^ & * ( ) _ - + = 系统:ESC、空格键、ENTER

    对于其他键,您应该绑定到 keydownkeyup

    Quirksmode.org compatibility reference 声明:

    当用户按下箭头键等特殊键时,浏览器不应触发按键事件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-03
      相关资源
      最近更新 更多