vue在文本框内指定位置添加字符串

这篇文章展示了如何在Vue.js应用中使用textarea组件,监听用户输入和点击事件。通过`@change`和`@click`事件处理函数,获取光标位置并插入特定文本(如#客户昵称#)。同时,文章提到了正则表达式用于替换字符串中的模式(如%NICKNAME%),以实现内容的动态转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

          <a-textarea
            style="width: 50%"
            ref="textarea"
            @change="handleWatchContent"
            @click="hanldeGetPosition"
            class="textarea"
            v-decorator="['workWeixinWelcomeMsg']"
          >
          </a-textarea>

          <a-button class="custonBtn" @click="handleAddNickName" size="small">
            #点击插入客户昵称#
          </a-button>
    handleWatchContent() {
      const textareaContent = this.$refs.textarea.$el.selectionStart;
      console.log(textareaContent);
      this.position = textareaContent;
    },

    hanldeGetPosition() {
      const textareaContent = this.$refs.textarea.$el.selectionStart;
      this.position = textareaContent;
    },

    handleAddNickName() {
      let content = "";
      if (this.$refs.textarea.$el._value) {
        content = this.$refs.textarea.$el._value;
      } else {
        content = "";
      }
      const value = content.split("");
      const nickName = ["#客户昵称#"];
      value.splice(this.position, 0, ...nickName);
      const textContent = value.join("");
      this.$refs.textarea.$el._value = textContent;
      this.$nextTick(() => {
        this.form.setFieldsValue({
          workWeixinWelcomeMsg: textContent,
        });
      });
    },
        const Regex = item.text.content
        item.text.content = Regex.replace(/%NICKNAME%/gi, '#客户昵称#')
        return item.text.content

正则转换

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值