按键事件在 IE 和 Chrome 中不起作用,但在 FF 中起作用
知道为什么会发生这种情况吗?我通常会认为 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将
keypress
更改为keydown
:按下按键时会发生 keydown 事件,紧接着发生 keypress 事件。然后当松开按键时会产生 keyup 事件。
Change
keypress
tokeydown
: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.
在这里找到答案: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.
是的,这可以通过将
onkeypress
事件更改为onkeydown
来实现。此外,仅当您想要在单击空格时触发事件时,才在 Internet Explorer 中存在此问题。yes this can be achieved by changing
onkeypress
event toonkeydown
. Moreover this issue exists just in case of Internet Explorer when you want to trigger an event on space click.