JS的focus()获得文本框焦点后,光标位置如何跳到文本末尾?

2024-11-03 20:03:09
推荐回答(3个)
回答(1):



function moveEnd(obj) {
obj.focus();
var len = obj.value.length;
if (document.selection) {
var sel = obj.createTextRange();
sel.moveStart('character', len);
sel.collapse();
sel.select();
} else if (typeof obj.selectionStart == 'number'
&& typeof obj.selectionEnd == 'number') {
obj.selectionStart = obj.selectionEnd = len;
}
}

回答(2):

获得文本框的长度n,然后用js在文本框加入n个 (空格)不就行了

回答(3):

const obj = document.getElementById('id')
if (obj) {
const value = obj.value;
obj.value = ''
obj.focus()
obj.value = value;
}