我有以下代码:
<div #test (click)="onClick(test)" contenteditable="true">
This text can be edited by the user.
</div>
@ViewChild('test') el:ElementRef;
constructor(private elementRef: ElementRef) {}
onClick(event) {
console.log(this.el.nativeElement);
let cursor = this.el.nativeElement.selectionStart; //gives undefined
}
当用户点击文本时,如何获取'div'元素的光标位置?
答案 0 :(得分:1)
selectionStart
是HTMLInputElement的属性。
<div #test (click)="onClick(test)" contenteditable="true">
This text can be edited by the user.
</div>
to
<input #test (click)="onClick(test)"
value='This text can be edited by the user.' />
希望这会有所帮助!!