var elInput = document.getElementById("expressionText");
var startPos = elInput.selectionStart;
var endPos = elInput.selectionEnd;
if (startPos === undefined || endPos === undefined) return
var txt = elInput.value;
var result = txt.substring(0, startPos) +record.idxKey + txt.substring(endPos)
elInput.value = result;
elInput.focus();
elInput.selectionStart = startPos + record.idxKey.length;
elInput.selectionEnd = startPos + record.idxKey.length;
封装成一个方法
selectStartContext(id,text){
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) +text + txt.substring(endPos)
elInput.value = result;
elInput.focus();
elInput.selectionStart = startPos + text.length;
elInput.selectionEnd = startPos + text.length;
},
获取光标位置后 放置内容 然后再把光标放到后面
const myField = document.querySelector('#'+id);
if(myField.selectionStart || myField.selectionStart === 0) {
let startPos = myField.selectionStart;
let endPos = myField.selectionEnd;
this.$set(this.model,'expression',myField.value.substring(0, startPos) + text
+ myField.value.substring(endPos, myField.value.length))
this.$nextTick()
myField.focus();
myField.setSelectionRange(endPos + text.length, endPos + text.length);