前端文本框插入标签(错误新增红色错误文字)

近期产品需求遇到一个问题,需要在查询输入框输入内容之后,需要 提示由后端返回不匹配文字,并且标红

有几种解决方案

1、最简单的方法是在输入框外,如它的下面新增 错误提示文字信息,最后正则匹配替换高亮错误文字

2、使用富文本的方式,需引入第三方富文本组件且需去除无关功能,然后在去正则匹配后端返回的文字,然后再新增带有style的标签即可,

3、使用使用障眼法,在textarea上面定位一个div并使用v-html富文本渲染,使用pointer-events: none;进行穿透可输入处理,且也可以同步到光标的位置,个人觉得是比较好的一种方法

实现:

<template>
  <div>
    <div class="addBox"  @click="hideRed">
      <el-input class="editInput" v-model="content" :maxLength="100" placeholder="请输入"
        type="textarea">
      </el-input>
      <!-- 高亮遮罩处理 -->
      <div class="editDiv textareaClas" v-html="contentHtml" contentEditable="true" v-if="contentHtml"> //contentEditable 是为了使其提供光标,然后通过点击可以渗透到textarea进行错误的修正
      </div>
    </div>
    <p  @click="addRed">请求接口</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      content: '',
      contentHtml: ''
    };
  },
  methods: {
    hideRed() {
      //这里的话 如果点击该div,就会隐藏掉遮罩内容
      if (this.contentHtml) {
        this.contentHtml = ''
      }
    },
    addRed() {
      let text = this.content;
      ['哈哈'].forEach(item => {
        // 使用 `new RegExp()` 创建正则表达式,并且用变量 item 的值替换里面的占位符  
        const regex = new RegExp(item, 'g'); // 如果 item 可能包含正则表达式特殊字符,需要额外转义  
        text = text.replace(regex, `<span style="color:red;">${item}</span>`);
      });
      // 需要正对于 < 符号的 浏览器不会渲染出<后面的,如<span style="color:red;">>123</span>,<span style="color:red;">外评级>AAA</span>,评级<AAA,这里<AAA就不会正常渲染
      let regex1 = /(?<!<span[^>]*)(<)(?![^>]*>)/g;  
      this.contentHtml = text.replace(regex1, '&lt;');  
     
    },

  }
};
</script>
<style scoped >
.addBox {
  position: relative;//父
	
  .editInput {
    font-size: 12px;
  }

  .editDiv {
    position: absolute;
    top: 0px;
    z-index: 99;
    background: transparent;
  }
    //和textarea样式一样,才能无感
  .textareaClas {
    pointer-events: none;
    resize: vertical;
    height: 65px;
    padding: 5px 15px;
    box-sizing: border-box;
    width: 100%;
    color: #606266 !important;
    background-image: none;
    border: 1px solid #DCDFE6;
    letter-spacing: normal;
    word-spacing: normal;
    line-height: normal;
    text-transform: none;
    text-indent: 0px;
    text-shadow: none;
    display: block;
    text-align: start;
    appearance: auto;
    -webkit-rtl-ordering: logical;
    cursor: text;
    overflow-wrap: break-word;
    background-color: field;
    column-count: initial !important;
    font-size: 12px;
    border-radius: 4px;
    font-family: monospace;
    transition: border-color .2s cubic-bezier(.645, .045, .355, 1);

    &:hover,
    &:focus,
    &:focus-visible {
      border: 1px solid #40a9ff !important;
    }
  }

  [contenteditable]:focus {
    outline: none;
  }

}
</style>

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在VSCode中,HTML标签的快捷键可以帮助开发者更高效地编写HTML代码。以下是一些常用的HTML标签快捷键: 1. **插入开始标签(自动闭合)**: `Ctrl + Shift + I` 或 `Cmd + Shift + I` (Mac)。例如,输入 `i` 然后按回车会在当前光标位置插入一个带有自动闭合的`<div>`标签。 2. **插入结束标签**: `Ctrl + Shift + K` or `Cmd + Shift + K` (Mac),在已经插入的开始标签内快速添加对应的结束标签。 3. **自定义标签**: `Ctrl + Shift + T`,用于快速插入自定义的HTML标签,通常需要预先在设置中配置或安装扩展如"Live HTML snippets"。 4. **删除标签对**: `Ctrl + Shift + X`,当光标在开始标签或结束标签内部时,可以同时删除整个开始和结束标签对。 5. **缩进和取消缩进**: `Tab` 和 `Shift + Tab` 可以调整HTML代码块的缩进。 6. **选择标签**: `Ctrl + Space`(在标签名上按住)可以选择标签及其内容,或者配合其他键进行操作,如 `Ctrl + Shift + Space` 查看文档。 7. **查找和替换标签**: `Ctrl + F` 打开搜索框,可以查找或替换HTML标签。 8. **标签提示**: 输入部分标签名后,按 `Ctrl + Space`(Windows/Linux)或 `Cmd + Space` (Mac) 可显示相关的标签建议。 以上快捷键适用于大多数基于VSCode的主题和插件,但具体可能会根据你的设置和所使用的插件有所不同。你可以根据个人喜好在VSCode的用户设置中调整这些功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值