gpt4 book ai didi

jquery - jQuery keypress、keydown、keyup 黑魔法(在 Mac 上)背后的理论是什么?

转载 作者:行者123 更新时间:2023-12-03 21:27:05 25 4
gpt4 key购买 nike

我对 keypresskeydownkeyup 的各种行为感到困惑。我似乎错过了一份重要的文档,它解释了这三者的微妙之处和细微差别。有人可以帮助我找出我需要阅读哪些文档才能更有效地使用这些事件吗?如果您想了解详细信息,请参阅下文。

@o.v.:您要求我显示一些代码,但这并不是我要解决的代码中的具体问题。我正在尝试了解这些事件处理程序的行为,并请了解它们的人为我指点一份好的文档。

我使用 jQuery 构建输入表单并将其插入到我的文档中。大多数情况下它工作得很好。我希望表单像我看到的大多数其他输入表单一样响应键盘: esc 键应该像单击取消按钮一样关闭表单,因为表单有一个 <textarea>在其上,cmd + enter 应该与单击确定按钮相同。使用 keypress 事件似乎很简单。问题是 Chrome 不会为 esc 键或 cmd + enter 调用我的 keypress 处理程序。它针对 ctrl + enteroption + enter 以及字母数字触发,但不针对 cmd + 输入

所以我将使用keyup来代替。我得到esckeyupcmdkeyup,以及keyup kbd>输入,太好了。但当我按住 cmd 时,我没有得到 enter 键的 keyup

第三次是魅力所在,您可能认为keydown似乎有效,但是使用keydown,您可以获得重复键。我知道,您所要做的就是在第一次调用时取消绑定(bind)处理程序,但三种不同的事件类型的行为如此不同似乎很奇怪。为什么是这样?是否有一份我显然没有读过的明显文件?

最佳答案

<强> Keypress :

<小时/>

The keypress event is sent to an element when the browser registers keyboard input. This is similar to the keydown event, except in the case of key repeats. If the user presses and holds a key, a keydown event is triggered once, but separate keypress events are triggered for each inserted character. In addition, modifier keys (such as Shift) trigger keydown events but not keypress events.

<强> Keydown :

<小时/>

The keydown event is sent to an element when the user first presses a key on the keyboard. It can be attached to any element, but the event is only sent to the element that has the focus. Focusable elements can vary between browsers, but form elements can always get focus so are reasonable candidates for this event type.

<强> Keyup :

<小时/>

The keyup event is sent to an element when the user releases a key on the keyboard. It can be attached to any element, but the event is only sent to the element that has the focus. Focusable elements can vary between browsers, but form elements can always get focus so are reasonable candidates for this event type.

此外,这是一条通常被掩盖的方便信息:

<小时/>

If key presses anywhere need to be caught (for example, to implement global shortcut keys on a page), it is useful to attach this behavior to the document object. Because of event bubbling, all key presses will make their way up the DOM to the document object unless explicitly stopped.

To determine which character was entered, examine the event object that is passed to the handler function. While browsers use differing properties to store this information, jQuery normalizes the .which property so you can reliably use it to retrieve the character code.

Note that keydown and keyup provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, a lowercase "a" will be reported as 65 by keydown and keyup, but as 97 by keypress. An uppercase "A" is reported as 65 by all events. Because of this distinction, when catching special keystrokes such as arrow keys, .keydown() or .keyup() is a better choice.

有关 MAC 上 cmd 键的更多信息:jQuery key code for command key

关于jquery - jQuery keypress、keydown、keyup 黑魔法(在 Mac 上)背后的理论是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12827408/

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