【问题标题】:$(document).on("keypress" - Not triggered in Firefox$(document).on("keypress" - 在 Firefox 中未触发
【发布时间】:2015-11-30 10:07:22
【问题描述】:

代码:

$(document).on("keypress", ".className", function (e) {
     debugger;
     if (e.which == 13) {
     debugger;
           ....
           ...
    }
}

以上代码在IE、CHROME 中触发,但在FIREFOX 中不起作用

注意: 我已经参考了以下与此相关的 SO 链接,但没有什么能让 firefox 触发..

onKeyPress event not working in Firefox

Key press event is not working in Mozilla Firefox

keypress is not working in Mozila Firefox

【问题讨论】:

  • 假设代码末尾缺少的) 是一个错字,它可以正常工作:jsfiddle.net/zq2tefa6 - 刚刚在 FF39 中测试
  • 它对我不起作用..
  • 很明显,您没有显示的一些代码是问题所在,适用于 firefox 42 和 44
  • 我也只在 Firefox 中得到 ReferenceError: event is not defined:jsfiddle.net/6knunwLp
  • 在IE / Chrome 中运行良好

标签: javascript jquery firefox browser keypress


【解决方案1】:

您应该在 Firefox 中使用 e.charCode。

$(document).on("keypress", ".className", function (e) {
     var code = e.charCode || e.keyCode;
     if (code == 13) {
     debugger;
           ....
           ...
    }
});

【讨论】:

  • e.which 的任何问题。
  • 问题本身的第一行debugger代码不会被触发。
  • 你能为此制作一个小提琴吗?
  • 此函数位于document.ready... 函数下方。在IE / Chrome中工作正常@
猜你喜欢
  • 1970-01-01
  • 2020-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多