jQuery keyup、keydown、keypress 和 input 事件之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38502560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:11:11  来源:igfitidea点击:

What's the difference between keyup, keydown, keypress and input events?

jqueryinputjavascript-events

提问by Tony Vincent

I have been trying to understand jQuery keypress, keydown, keyup and inputevents. But I found them quite confusing. Could someone please point out the exact differences ? Also I would like to know do all of them get triggered when the user paste a piece of text .
Thanks

我一直在尝试了解 jQuery 按键、按键、按键和输入事件。但我发现它们很混乱。有人可以指出确切的区别吗?另外我想知道当用户粘贴一段文本时,所有这些都会被触发。
谢谢

回答by dodopok

According to jQuery docs:

根据jQuery 文档

The keypressevent is sent to an element when the browser registers keyboard input. This is similar to the keydownevent, except that modifier and non-printing keys such as Shift, Esc, and delete trigger keydown events but not keypress events. Other differences between the two events may arise depending on platform and browser.

The keyupevent is sent to an element when the user releases a key on the keyboard.

The oninputevent it's an event that triggers whenever the input changes.

按键事件被发送到当浏览器登记键盘输入的元件。这类似于keydown事件,不同之处在于修饰符和非打印键(例如 Shift、Esc 和 delete 会触发 keydown 事件而不是 keypress 事件)。根据平台和浏览器的不同,可能会出现两个事件之间的其他差异。

所述KEYUP事件被发送到当用户释放键盘上的一个键的元素。

oninput事件这是一个事件触发,只要输入的变化。

However the input event is not supported in IE version below 9. In that case, you could use proprietary event onpropertychange, it does the same as oninput.

但是,IE 9 以下版本不支持输入事件。在这种情况下,您可以使用专有事件 onpropertychange,它的作用与 oninput 相同。

But in your case, you could use the pasteand changeevent together. You should use changetoo because pasteonly happens on browsers that support it on an explicit paste.

但在您的情况下,您可以同时使用pastechange事件。您也应该使用更改,因为粘贴仅发生在支持显式粘贴的浏览器上。