就是获取光标位置 然后截取拼接字符串 重新填入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;
}
1 回答
尚方宝剑之说
TA贡献1788条经验 获得超4个赞
就是获取光标位置 然后截取拼接字符串 重新填入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; }
添加回答
举报
0/150
提交
取消