Why can I preventDefault event on keydown but not on keyup with Javascript?

后端 未结 2 1199
独厮守ぢ
独厮守ぢ 2021-01-03 02:29

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

When using .keyup I cannot?

2条回答
  •  被撕碎了的回忆
    2021-01-03 02:36

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

    Whenever you type a character the following events occur:

    keydown --> keypress repeatedly until the key is released --> keyup

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

提交回复
热议问题