方法一
input.setSelectionRange(opsStart,opsEnd);
setSelectionRange方法用于设定<input>
或 <textarea>
元素中当前选中文本的起始和结束位置。
当opsStart和opsEnd相同时,可用于定位光标位置。
setSelectionRange实际上是改变了selectionStart 和 selectionEnd属性,所以也可以通过直接修改这两个属性来实现
方法二
input.selectionStart = opsStart;
input.selectionEnd = opsEnd;
展示效果
function click1() { // 光标前移一位
let input = document.querySelector('input');
let selectionStart = input.selectionStart;
i