swa*_*990 5 html typescript angular
我有以下代码:
<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
}
Run Code Online (Sandbox Code Playgroud)
当用户单击文本时,如何获取“ div”元素的光标位置?
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.' />
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!!
| 归档时间: |
|
| 查看次数: |
1774 次 |
| 最近记录: |