use*_*485 9 keyboard-events angular2-forms angular
我需要从选定的 div 输入键触发一个功能。我写了下面的代码,但它不起作用。
<div class="data_card" *ngIf="!Add.showEdit" (keyup.enter)="myfunction($event)">
<!-- Some content -- >
</div>
myfunction($event){
$event.preventDefault();
alert("sadsa");
}
Run Code Online (Sandbox Code Playgroud)
如何从按下 div enter 键触发此功能?
试试这个代码。这有帮助吗。 html
<div class="data_card" *ngIf="!Add.showEdit" (keydown)="handleKeyboardEvent($event)">
Run Code Online (Sandbox Code Playgroud)
// 成分
@HostListener('document:keydown', ['$event'])
handleKeyboardEvent(event: KeyboardEvent): void {
if (event.keyCode === 13) {
**// do your code here**
}
}
Run Code Online (Sandbox Code Playgroud)
看看这个答案。如何将键盘事件绑定到 div 元素?.
使用标签索引:
The tabindex global attribute is an integer indicating if the element can take input focus (is focusable)
| 归档时间: |
|
| 查看次数: |
12713 次 |
| 最近记录: |