项目中使用了自己写数字键盘,想在一堆数字中插入键盘值,然后光标定位在插入值后面
const contentRow= document.getElementById("contentRow")
const selection = getSelection();
const range = selection.getRangeAt(0);
range.setStart(contentRow.childNodes[0], offsetLength);
range.setEnd(contentRow.childNodes[0], offsetLength);
range.collapse(true);
selection.removeAllRanges();
selection.addRange(range);
offsetLength是插入值后的index值
然后发现光标一直在第一位,没法到offsetLength,后来试着用setTimeout才能把它定位到那里,但是这样会有一点停顿的感觉。有没有更好的解决方法??