替换过时的“ keypress” DOM事件 [英] Replacement for deprecated `keypress` DOM event

查看:97
本文介绍了替换过时的“ keypress” DOM事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据



但是我在其他地方找不到关于是否应在新项目中使用此事件的任何信息。如果我们不应该,那么替换的是什么?



有人可以提供一个见识吗?

解决方案

由于不建议使用该事件,因此应避免在新代码中使用它,并计划将其从旧代码中删除。 W3C规范说明了不推荐使用的功能:


标记为已弃用的功能已包含在规范中,以作为对较早实现或规范的引用,但是可选的,不鼓励使用。在本规范中,仅应弃用具有现有或正在进行的替换功能。尚不包括对功能的支持的实现可能会出于与现有内容向后兼容的原因而实现不推荐使用的功能,但是创建内容的内容作者不应该使用不推荐使用的功能,除非没有其他方法可以解决用例强>。引用此规范的其他规范不应使用不推荐使用的功能,而应指出赞成不推荐使用该功能的替换。


keypress事件 表示:


警告此规范中定义了按键事件类型,以供参考和完整性,但不推荐使用使用此事件类型。在编辑上下文中,作者可以订阅 beforeinput 事件


您还可以使用 keydown 和/或 keyup 事件。参见 keyup,keydown,keypress和输入之间有什么区别事件?



但是,由于 beforeinput 尚无太多支持,如果这些都不支持其他事件适合您的用例,您现在必须继续使用 keypress (这是规范中的除非没有其他方法可以解决用例例外) )。


According to MDN article keypress event is deprecated:

But I can't find any information elsewhere on whether we should use this event in a new projects. If we shouldn't, what is the replacement?

Could somebody give an insight?

解决方案

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).

这篇关于替换过时的“ keypress” DOM事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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