<el-input type="textarea" v-model="formData.val" placeholder="請輸入"
@keydown.delete.native="onDeleteKeyDown" @keydown.native="onKeyDown"
@compositionstart.native="onCompositionStart" @compositionend.native="compositionend"></el-input>
onDeleteKeyDown(e) {
let { selectionStart, selectionEnd } = e.target;
// 如果包含不能刪除的區(qū)域位置,阻止刪除
if (!(selectionStart > 7 || selectionEnd < 0)) {
e.preventDefault();
}
},
onKeyDown(e) {
// 如果包含不能刪除的區(qū)域锻霎,禁止
if (!(e.target.selectionStart > 6 || e.target.selectionEnd < 0)) {
e.preventDefault();
}
},
// 輸入法鍵盤字符下不可輸入抛蚁,記錄輸入的起始位置
onCompositionStart(e) {
this.checkText = e.data; //記錄選中的文字
this.startVal = e.target.selectionStart;
},
// 當在不可編輯的范圍內(nèi)輸入是昙沦,結(jié)合輸入文字開始位置this.startVal和輸入結(jié)束
// e.target.selectionEnd位置,通過字符串截取并接還原原來的字符您旁。
compositionend(e) {
if (this.startVal !== null) {
this.$nextTick(() => {
if (this.startVal < 7) {
let targetVal = e.target.value;
let startTarget = targetVal.substring(0, this.startVal);
let endTarget = targetVal.substring(e.target.selectionEnd);
e.target.value = startTarget + this.checkText + endTarget;
}
});
}
},