Why doesn't keypress handle the delete key and the backspace key

牧云@^-^@ 提交于 2019-11-28 07:37:56

问题


I'm not asking this because I need a work-a-around. I have one that works fine, but I want to know WHY it doesn't. Is this bug in javascript (or JQuery because I was using the JQuery .keypress handler) or is there a specific reason why this is so?

Thanks in advance.


回答1:


The keypress event is designed for handling a character typed by the user rather than detecting keyboard activity and the delete and backspace keys do not generate characters. Some browsers blur this line somewhat but the general principle is that the keyup and keydown events are there to detect any key being pressed and telling you about which key it is while keypress is for detecting an actual character being typed.




回答2:


The short answer is that the onkeypress event is not fired for all key types in all browsers. Check the manual for your browser.

Why?

Probably not a comprehensive answer, but consider Shift, when it goes down and when it comes up relative to other keys is significant. And different keyboard hardware has different key rollover characteristics which you might want to know about in detail.



来源:https://stackoverflow.com/questions/3911589/why-doesnt-keypress-handle-the-delete-key-and-the-backspace-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!