onKeyPress 对比onKeyUp 和 onKeyDown [英] onKeyPress Vs. onKeyUp and onKeyDown
问题描述
这三个事件有什么区别?谷歌搜索后我发现:
What is the difference between these three events? Upon googling I found that:
onKeyDown
事件在用户按下某个键时触发.onKeyUp
事件在用户松开按键时触发.onKeyPress
事件在用户按下 & 键时触发.释放钥匙(onKeyDown
后跟onKeyUp
).
- The
onKeyDown
event is triggered when the user presses a key.- The
onKeyUp
event is triggered when the user releases a key.- The
onKeyPress
event is triggered when the user presses & releases a key (onKeyDown
followed byonKeyUp
).
我理解前两个,但 onKeyPress
和 onKeyUp
不一样吗?是否可以在不按下键 (onKeyDown
) 的情况下释放键 (onKeyUp
)?
I understand the first two, but isn't onKeyPress
the same as onKeyUp
? Is it possible to release a key (onKeyUp
) without pressing it (onKeyDown
)?
这有点令人困惑,有人可以帮我解决这个问题吗?
This is a bit confusing, can someone clear this up for me?
推荐答案
在此处查看存档的 link 最初用于此答案.
Check here for the archived link originally used in this answer.
来自该链接:
理论上,onKeyDown
和 onKeyUp
事件代表按键被按下或释放,而 onKeyPress
事件代表正在输入的字符.该理论在所有浏览器中的实现并不相同.
In theory, the
onKeyDown
andonKeyUp
events represent keys being pressed or released, while theonKeyPress
event represents a character being typed. The implementation of the theory is not same in all browsers.
这篇关于onKeyPress 对比onKeyUp 和 onKeyDown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!