How to disable a key when a button is focused

我的梦境 提交于 2019-12-13 04:19:15

问题


How to disable the up arrow key when a button is focused and enable it when the button is not focused in angular2? For example:

In html:

<button type="button" (focus)="disableUpArrowKey()">

In script:

disableUpArrowKey(){
    //??
}

回答1:


Bind on keydown and call preventDefault of event.

html:

<button (keydown)="onKeydown($event)">Button</button>

ts:

...
onKeydown(event: KeyboardEvent) {
  if (event.key === 'ArrowUp') {
    event.preventDefault()
  }
}
...

stackblitz : https://stackblitz.com/edit/angular-xmzct5



来源:https://stackoverflow.com/questions/54014662/how-to-disable-a-key-when-a-button-is-focused

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!