为什么我可以在keydown上阻止默认事件但不能在使用Javascript的keyup上阻止? [英] Why can I preventDefault event on keydown but not on keyup with Javascript?

查看:577
本文介绍了为什么我可以在keydown上阻止默认事件但不能在使用Javascript的keyup上阻止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 .keydown 时,我可以捕获keydown事件,然后检查并阻止默认操作(显示字符)。

When using .keydown I can capture keydown event, then check and prevent default action (display the character).

使用 .keyup 我不能?

我知道该事件被捕获为 alert()当代码在条件内时触发,但 preventDefault()不会阻止该操作。

I know the event is being captured as alert() fires when the code is inside the condition yet the preventDefault() doesn't prevent the action.

这是一个完整的 DEMO

推荐答案

keyup 事件中,该字符已被输入且无法撤消但在 keydown 没有输入任何内容且浏览器有意图来键入字符,因此您可以取消浏览器意图。

In keyup event the character has been typed and can't be undone but in keydown no things has been typed and the browser has intent to type the character, so you can cancel the browser intent.

每当您输入字符时,会发生以下事件:

keydown - >( keypress - >(输入操作))重复发生,直到密钥被释放 - > keyup

Whenever you type a character the following events occur:
keydown ->( keypress -> (typing action)) repeatedly occurs until the key is released -> keyup


  • keydown - >可以阻止 - >在以下时触发:

  • keypress - >可以阻止 - >在以下时触发:按住一个键

  • keyup - > 可以阻止 - >在发布密钥时触发

  • keydown -> can be prevented -> fired when: press a key
  • keypress -> can be prevented -> fired when: hold a key
  • keyup -> can not be prevented -> fired when: release a key

这篇关于为什么我可以在keydown上阻止默认事件但不能在使用Javascript的keyup上阻止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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