javascript - 替换已弃用的 `keypress` DOM 事件

标签 javascript html events dom dom-events

根据 MDN article keypress 事件已弃用:

enter image description here

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

有人可以提供见解吗?

最佳答案

由于该事件已被弃用,您应该避免在新代码中使用它,并计划将其从旧代码中移除。 W3C specification说到弃用的功能:

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.

keypress event 的规范说:

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.

您还可以使用 keydown和/或 keyup 事件。参见 What's the difference between keyup, keydown, keypress and input events?

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

关于javascript - 替换已弃用的 `keypress` DOM 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52882144/

相关文章:

javascript - MYSQL 通过按钮排列/重组数据库

Html 垂直菜单,子菜单不显示在父菜单下方

javascript - 用 JavaScript 编写自定义事件调度程序?

javascript - 通过php显示javascript内容

javascript - 如何在 JavaScript 中打印出对象的所有属性以及分配给它们的值?

javascript - 克隆 <li> 并使用 jQuery 抛出

javascript - 如何仅在用户开始向下滚动后才显示标题? [ react ]

html - 适用于所有主要浏览器的视频编解码器

javascript - 动态分配事件监听器

jquery - jquery 选择器上的 not() 方法不起作用