问题
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