Since the event is deprecated, you should avoid using it in new code, and plan on removing it from old code. The W3C specification says this about deprecated features:
Features marked as deprecated are included in the specification as reference to older implementations or specifications, but are OPTIONAL and discouraged. Only features which have existing or in-progress replacements MUST be deprecated in this specification. Implementations which do not already include support for the feature MAY implement deprecated features for reasons of backwards compatibility with existing content, but content authors creating content SHOULD NOT use deprecated features, unless there is no other way to solve a use case. Other specifications which reference this specification SHOULD NOT use deprecated features, but SHOULD point instead to the replacements of which the feature is deprecated in favor. Features marked as deprecated in this specification are expected to be dropped from future specifications.
The specification of the keypress event
says:
Warning The keypress event type is defined in this specification for reference and completeness, but this specification deprecates the use of this event type. When in editing contexts, authors can subscribe to the beforeinput event instead.
You can also use the keydown
and/or keyup
events. See What's the difference between keyup, keydown, keypress and input events?
However, since beforeinput
doesn't yet have much support, if none of these other events fits your use case you'll have to continue to use keypress
for now (that's the "unless there is no other way to solve a use case" exception in the spec).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…