gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:17:08 27 4
gpt4 key购买 nike

根据 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com