Javascript 选择开始不更新输入字段的焦点
在我的应用程序中,我有一个输入字段。我正在使用输入元素的Javascript 选择开始不更新输入字段的焦点,javascript,html,events,onfocus,Javascript,Html,Events,Onfocus,在我的应用程序中,我有一个输入字段。我正在使用输入元素的selectionStart属性。当我们在#######keyup事件上获得selectionStart值时,一切都正常工作。但是当我在输入元素的焦点上使用selectionStart时,它不会更新,即获取值0。我找不到原因 JS: onKeyup(event) { let selectionIndex = event.target.selectionStart; console.log(selectionIndex); }
selectionStart
属性。当我们在#######keyup
事件上获得selectionStart
值时,一切都正常工作。但是当我在输入元素的焦点上使用selectionStart
时,它不会更新,即获取值0
。我找不到原因
JS:
onKeyup(event) {
let selectionIndex = event.target.selectionStart;
console.log(selectionIndex);
}
<section>
<input [(ngModel)]="enteredText" (focus)="onKeyup($event)" (keyup)="onKeyup($event)"/>
</section>
HTML:
onKeyup(event) {
let selectionIndex = event.target.selectionStart;
console.log(selectionIndex);
}
<section>
<input [(ngModel)]="enteredText" (focus)="onKeyup($event)" (keyup)="onKeyup($event)"/>
</section>
请帮我解决这个问题
谢谢…为什么不给输入元素一个
id
,然后在键控时使用document.getElementById('elementId')
查找它,然后获得selectionStart
属性不确定,但尝试不使用argonKeyup(){let selectionIndex=event.target.selectionStart;console.log(选择索引);}
在jsIt中工作正常。检查这个-@AdityaBhave,在stackblitz中复制错误。在焦点上,开始和结束索引仅为0,0。请检查。@Allamogusu,我也使用了它。仍然存在相同的问题。为什么不给输入元素一个id
,然后在keyup上使用document.getElemen查找它tById('elementId')
之后,您将获得selectionStart
属性不确定,但请尝试不使用argonKeyup(){让selectionIndex=event.target.selectionStart;console.log(selectionIndex);}
在jsIt中工作正常。检查这个-@AdityaBhave,在stackblitz中复制错误。在焦点上,开始和结束索引仅为0,0。请检查。@Allamogusu,我也使用了它。仍然得到相同的问题。