el-input textarea 禁止输入中文字符,@input特殊字符实时替换,光标位置保持不变

使用<el-input type="textarea">组件来自Vue的Element UI库时,如果想在输入时实时监听输入并替换某些内容,同时又希望保持光标位置不变(即在内容被替换后光标不自动跳到内容的最后位置),可以通过以下方式来实现:

方法1:使用input事件和setSelectionRange方法

在Vue中,监听input事件,然后使用JavaScript的setSelectionRange方法来设置光标位置。

复制代码
<template>
  <el-input
    type="textarea"
    ref="myTextarea"
    v-model="inputText"
    @blur="handleblur"
    @input="handleInput"
  ></el-input>
</template>
 
<script>
export default {
  data() {
    return {
      inputText: ''
    };
  },
  methods: {
    handleInput() {
      // 获取光标位置
      let cursorPosition = this.$refs.myTextarea.$refs.textarea.selectionStart;
      // 对输入内容进行替换处理,这里以简单的中文逗号替换英文逗号为例
      this.inputText = this.inputText.replace(/,/g, ',');
      // 恢复光标位置
      this.$nextTick(()=>{
        this.$refs.myTextarea.$refs.textarea.setSelectionRange(cursorPosition, cursorPosition)
      })
    },
    handleblur() {
      // 失去焦点,处理多个逗号和连续超过两个逗号替换成只要一个逗号
      if(this.inputText) {
        this.inputText = this.inputText.trim().replace(/\s*(,)\s*/g, ',').replace(/,{2,}/g, ',')
      }
    },
  }
};
</script>
方法2:使用计算属性控制显示值和内部值分离

如果不在input事件中直接操作DOM,可以通过计算属性来控制显示的文本和实际的文本存储在不同的变量中。

复制代码
<template>
  <el-input
    type="textarea"
    ref="myTextarea"
    v-model="inputText"
    @blur="handleblur"
    @input="handleInput"
  ></el-input>
</template>
 
<script>
export default {
  data() {
    return {
      inputText: '', // 显示给用户的文本,可能经过处理或未处理
      internalText: '', // 实际存储的文本
    };
  },
  watch: {
    internalText(newVal) {
      this.inputText= newVal; // 确保视图更新时,显示的文本也是最新的内部文本
    }
  },
  methods: {
    handleInput() {
      // 处理文本
      let newValue = this.$refs.myTextarea.$refs.textarea.value.replace(/,/g, ',');
      // 获取光标位置
      let cursorPosition = this.$refs.myTextarea.$refs.textarea.selectionStart;
      // 更新显示值(但不触发渲染)
      this.$refs.myTextarea.$refs.textarea.value = newValue;
      // 恢复光标位置
      this.$nextTick(()=>{
        this.$refs.myTextarea.$refs.textarea.setSelectionRange(cursorPosition, cursorPosition)
      })
      // 更新内部存储的文本值,不会触发视图更新,除非通过watch监听器
      this.internalText = newValue;
    },
    handleblur() {
      // 失去焦点,处理多个逗号和连续超过两个逗号替换成只要一个逗号
      if(this.inputText) {
        this.inputText = this.inputText.trim().replace(/\s*(,)\s*/g, ',').replace(/,{2,}/g, ',')
      }
    },
  }
};
</script>
相关推荐
开开心心就好6 小时前
批量提取PDF中的图片,直接导出原图
前端·javascript·支持向量机·智能手机·pdf·html·启发式算法
Setsuna_F_Seiei8 小时前
前端转型 Agent 开发 05 之 Agent Hooks 与 Checkpointer(让 Agent 从全自动转变人为可掌控)
前端·agent·ai编程
百万蹄蹄向前冲10 小时前
风扇转了一晚上MVP专家团翻车事故
前端·人工智能
默_笙10 小时前
🏛 给 AI 配一间办公室:Harness Engineering 六大模块与它的实现
前端·javascript
linux_cfan11 小时前
videojs v10 源代码系列解读:14 · 谓词守卫:在运行时安全地调用能力
前端·javascript·音视频
kyriewen11 小时前
我扒了 10,221 条 JD:腾讯技术岗 75% 在要 AI
前端·人工智能·ai编程
郑州光合科技余经理12 小时前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程
+VX:Fegn089513 小时前
计算机毕业设计|基于springboot + vue旅游管理系统(源码+数据库+文档)
java·开发语言·vue.js·spring boot·课程设计
雪芽蓝域zzs13 小时前
第 7 章:双 Y 轴组合图(柱状 + 折线,看板高频场景)
vue.js·echars
IT_陈寒13 小时前
Vue的响应式让我熬到凌晨三点,原来漏了这个小细节
前端·人工智能·后端