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

发布于 2024-11-14 16:36:49 字数 274 浏览 4 评论 0原文

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

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

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

感谢您的帮助!

Any idea why this might happen? I would usually think that Chrome would be more forgiving with the codes?

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

Thats what my keypress key looks like. Am I missing something? rightImage(); and leftImage(); are functions which should be working becase I'm using those functions somewhere else too

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

匿名的好友 2024-11-21 16:36:49

keypress 更改为 keydown

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

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

为了理解 keydown 和 keypress 之间的区别,它
对于理解“字符”和“字符”之间的区别很有用
“钥匙”。 “键”是计算机键盘上的物理按钮,而
“字符”是通过按下按钮输入的符号。理论上,
keydown 和 keyup 事件代表按键被按下或释放,
而按键事件代表正在键入的字符。这
该理论的实现在所有浏览器中并不相同。

Change keypress to keydown:

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

The keydown event occurs when the key is pressed, followed immediately by the keypress event. Then the keyup event is generated when the key is released.

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.

打小就很酷 2024-11-21 16:36:49

在这里找到答案:http://api.jquery.com/keypress/

“如果你想在 Chrome 中使用箭头、删除、退格键,您必须使用 keydown,这些键仅在 Firefox 和 Opera 中有效。”

你的代码在 iE8 中不适合我(在 FF 中工作),所以我将 keypress 切换为 keydown。现在可以在 IE 中工作。这里没有 Chrome 来测试。

Found the answer here: http://api.jquery.com/keypress/

"If you want to use arrows, delete, backspace keys in Chrome you must use keydown. keypress on these keys work only in Firefox and Opera."

Your code didn't work for me in iE8 (worked in FF), so I switched keypress to keydown. Works in IE now. Don't have Chrome here to test.

苍景流年 2024-11-21 16:36:49

是的,这可以通过将 onkeypress 事件更改为 onkeydown 来实现。此外,仅当您想要在单击空格时触发事件时,才在 Internet Explorer 中存在此问题。

yes this can be achieved by changing onkeypress event to onkeydown. Moreover this issue exists just in case of Internet Explorer when you want to trigger an event on space click.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文