SOURCE

console 命令行工具 X clear

                    
>
console
let oTxt = document.getElementById('txt');
let oBtn = document.getElementById('btn');
let index = 0;
oBtn.onclick = function (e) {
    e.preventDefault();
    // if (oTxt.setSelectionRange) {
    //     oTxt.setSelectionRange(index, index);
    //     index++;
    //     if (index === oTxt.value.length + 1) {
    //         index = 0;
    //     }
    // }
    const selectionStart = oTxt.selectionStart;
    const firstStr = oTxt.value.substr(0, selectionStart);
    const lastStr = oTxt.value.substr(selectionStart);
    oTxt.value = firstStr + 123465 +lastStr;
    oTxt.selectionStart = selectionStart + 6;
    oTxt.selectionEnd = selectionStart + 6;
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
 
<body>
    <input type="text" value="测试的文字" id="txt">
    <button id="btn">设置焦点</button>
</body>
</html>