javascript 为什么按键不处理删除键和退格键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3911589/
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-10-25 02:40:06  来源:igfitidea点击:

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

javascriptkeypressjquery-eventsonkeypress

提问by aarona

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 .keypresshandler) or is there a specific reason why this is so?

我问这个不是因为我需要一个解决办法。我有一个可以正常工作的,但我想知道为什么不行。这是 Javascript(或 JQuery,因为我使用的是 JQuery.keypress处理程序)中的错误,还是有特定的原因?

回答by Tim Down

The keypressevent 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 keyupand keydownevents are there to detect any key being pressed and telling you about which key it is while keypressis for detecting an actual character being typed.

keypress事件旨在处理用户键入的字符,而不是检测键盘活动,并且删除和退格键不会生成字符。一些浏览器在某种程度上模糊了这条线,但一般原则是keyupkeydown事件在那里检测任何被按下的键并告诉您它是哪个键同时keypress用于检测正在输入的实际字符。

回答by Don

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

简短的回答是所有浏览器中的所有键类型都不会触发 onkeypress 事件。检查您的浏览器的手册。

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.

可能不是一个全面的答案,但考虑到 Shift,当它下降和上升时相对于其他键很重要。不同的键盘硬件具有不同的按键翻转特性,您可能想详细了解这些特性。