在光标位置插入文本,设置光标位置
<div class="color borderColor pointer" @click="addSymbol('+')">+</div> <div class="color borderColor pointer" @click="addSymbol('|')">|</div> <div class="color borderColor pointer" @click="addSymbol('()')">( )</div> <el-input style="width: 94%" @blur="blurEvent" size="small" placeholder="多个排除词之间以,隔开,词组之间为“与”关系" :rows="5" type="textarea" v-model="ruleForm.excludeWords"></el-input>
data() { return { blurIndex: null,//记录光标位置 ruleForm: { excludeWords: '' }, } },
methods: { // 获取光标所在位置的index blurEvent(e) { this.blurIndex = e.srcElement.selectionStart; console.log(e) console.log(e.srcElement) console.log(e.srcElement.selectionStart) //光标所在的位置 }, // 添加符号 addSymbol(symbol){ let index=this.blurIndex let str=this.ruleForm.excludeWords this.ruleForm.excludeWords=str.slice(0, index) + symbol + str.slice(index); this.blurIndex++ }, }
设置光标位置
set_text_value_position(obj, spos){ var tobj = document.getElementById(obj); if(spos<0) spos = tobj.value.length; if(tobj.setSelectionRange){ //兼容火狐,谷歌 setTimeout(function(){ tobj.setSelectionRange(spos, spos); tobj.focus();} ,0); }else if(tobj.createTextRange){ //兼容IE var rng = tobj.createTextRange(); rng.move('character', spos); rng.select(); } },
调用 参数一:input框dom的id,参数二:位置
// 调用设置光标位置 this.set_text_value_position('inputId',5)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库