知道为什么会发生这种情况吗?我通常认为 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/