keyup、keydown、keypress 和 input 事件之间有什么区别? [英] What's the difference between keyup, keydown, keypress and input events?

查看:32
本文介绍了keyup、keydown、keypress 和 input 事件之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试理解 jQuery keypresskeydownkeyupinput 事件.但我发现它们很混乱.有人可以指出确切的区别吗?另外我想知道当用户粘贴一段文本时是否所有这些都会被触发.

解决方案

根据 jQuery 文档:><块引用>

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

当用户释放键盘上的键时,keyup 事件被发送到一个元素.

oninput 事件是每当输入更改时触发的事件.

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

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

I have been trying to understand jQuery keypress, keydown, keyup and input events. 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.

解决方案

According to jQuery docs:

The keypress event is sent to an element when the browser registers keyboard input. This is similar to the keydown event, 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 keyup event is sent to an element when the user releases a key on the keyboard.

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

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.

But in your case, you could use the paste and change event together. You should use change too because paste only happens on browsers that support it on an explicit paste.

这篇关于keyup、keydown、keypress 和 input 事件之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆