jquery - 按键事件在 IE 和 Chrome 中不起作用,但在 FF 中起作用

标签 jquery internet-explorer firefox google-chrome keypress

知道为什么会发生这种情况吗?我通常认为 Chrome 对这些代码会更宽容?

$(document).keypress(function(e) {
    if(e.keyCode == 39) rightImage();
    if(e.keyCode == 37) leftImage();
});

这就是我的按键的样子。我错过了什么吗?右图();和 leftImage();这些函数应该可以工作,因为我也在其他地方使用这些函数

感谢您的帮助!

最佳答案

keypress更改为keydown:

$(document).keydown(function(e) {
    if(e.keyCode == 39) rightImage();
    if(e.keyCode == 37) leftImage();
});

按下按键时发生 keydown 事件,紧接着发生 keypress 事件。然后当松开按键时会产生 keyup 事件。

In order to understand the difference between keydown and keypress, it is useful to understand the difference between a "character" and a "key". A "key" is a physical button on the computer's keyboard while a "character" is a symbol typed by pressing a button. In theory, the keydown and keyup events represent keys being pressed or released, while the keypress event represents a character being typed. The implementation of the theory is not same in all browsers.

关于jquery - 按键事件在 IE 和 Chrome 中不起作用,但在 FF 中起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6311290/

相关文章:

php - 使用 AJAX 将数据回显到下拉列表中

css - 如何编写在特殊浏览器上运行的 css 规则?

excel - 引用 If 语句的 'onclick' 事件 - IE 自动化

python - 已通过 Selenium 使用 GeckoDriver Firefox 丢弃浏览上下文

javascript - 将 jQuery UI Slider 与 Checkbox 事件绑定(bind)的问题

javascript - 通过点击隐藏一个 DIV 并通过 JavaScript 使另一个 DIV 可见?

html - 如何修复 IE 6 错误元素垂直对齐?

javascript - 在 Firefox 中输入选择焦点的外观

Javascript 工具提示跨浏览器兼容性

jquery - 温度天气颜色变化