Js setting cursor position problem

The

project uses its own numeric keypad. I want to insert the keyboard value into a pile of numbers, and then position the cursor after the insertion value

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 is the index value after inserting the value
and then I find that the cursor is always in the first place. I can"t go to offsetLength, and then try to locate it there with setTimeout, but it feels a little pause. Is there a better solution?

Feb.23,2022
Menu