【发布时间】:2019-08-28 07:51:35
【问题描述】:
我在 div 中有一个文本输入。单击输入应将其设置为焦点并停止 div 单击事件的冒泡。我在文本输入事件上尝试了stopPropagation 和preventDefault,但无济于事。控制台日志显示 div click 仍然执行。如何停止div点击事件的执行?
// html
<div (click)="divClick()" >
<mat-card mat-ripple>
<mat-card-header>
<mat-card-title>
<div style="width: 100px">
<input #inputBox matInput (mousedown)="fireEvent($event)" max-width="12" />
</div>
</mat-card-title>
</mat-card-header>
</mat-card>
</div>
// component
@ViewChild('inputBox') inputBox: ElementRef;
divClick() {
console.log('click inside div');
}
fireEvent(e) {
this.inputBox.nativeElement.focus();
e.stopPropagation();
e.preventDefault();
console.log('click inside input');
return false;
}
【问题讨论】:
标签: javascript angular7 event-bubbling typescript3.0