[英]How set auto focus next in input type time
我有一個簡單的time
類型輸入,嘗試在DOM樹中設置自動聚焦下一個輸入
<input type="time" name="startTime" (keyup)="setFocus($event)"/>
setFocus(element) {
if (element.target.value.length == 5) {
let coll = document.getElementsByTagName('input');
let array = Array.from(coll)
let thisOne = array.forEach((el) => {
if (el == element.target) {
let index = array.indexOf(el)
let next = array[index + 1]
next.focus();
next.select();
}
});
}
}
問題是當時間具有默認值時,值的長度始終為5(在鍵入時),如05:45
或18:22
,那么如何設置下一個自動聚焦? 有什么辦法可以找出光標在第5位嗎? 任何想法?
知道您有輸入元素的ID后,就可以使用JavaScript焦點方法設置焦點,如下所示
document.getElementById(next.id).focus();
編輯:我發現如果元素沒有ID,則焦點將不起作用,如果您設置了元素ID,則您的代碼將可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.