就是获取光标位置 然后截取拼接字符串 重新填入input
重新设置光标
function insertInputTxt(id, insertTxt) { var elInput = document.getElementById(id); var startPos = elInput.selectionStart; var endPos = elInput.selectionEnd; if (startPos === undefined || endPos === undefined) return var txt = elInput.value; var result = txt.substring(0, startPos) + insertTxt + txt.substring(endPos) elInput.value = result; elInput.focus(); elInput.selectionStart = startPos + insertTxt.length; elInput.selectionEnd = startPos + insertTxt.length; }
尚方宝剑之说
相关分类