在input输入框光标位置插入内容

let input = document.getElementById('input')
let textToInsert = '我是插入的内容';
let startPosition = input.selectionStart;
let endPosition = input.selectionEnd;
input.value = input.value.substring(0, startPosition) + textToInsert + input.value.substring(endPosition, input.value.length);
input.setSelectionRange(startPosition + textToInsert.length, startPosition + textToInsert.length);
input.focus()

posted on 2023-04-02 15:00  hanguahannibk  阅读(131)  评论(0编辑  收藏  举报