Vue实现动态输入关键字高亮

文章介绍了如何在Vue和React项目中,使用VueRef和事件监听,实现实时文本输入框的高亮显示关键词并替换功能。同时,文本区域会同步滚动。
摘要由CSDN通过智能技术生成
<style scoped>
.main {
  width: 500px;
  margin-left: 100px;
  height: 500px;
  position: relative;
}
.common-style {
  width: 100%;
  height: 100%;
  font-size: 14px;
  font-family: monospace;
  word-break: break-all;
  letter-spacing: 1px; /* 设置字间距 */
  line-height: 1.5715;
}
/* div */
.highlight-shadow-input {
  position: absolute;
  top: 0;
  left: 0;
  margin-left: -100px;
  padding: 9px;
  border: 1px;
  box-sizing: border-box;
  overflow: auto;
  white-space: pre-wrap;
  text-align: left;
  vertical-align: bottom;
  z-index: 0;
}
.highlight-input {
  position: relative;
  padding: 5px 11px;
  display: block;
  margin-left: -100px;
  padding: 8px;
  box-sizing: border-box;
  background: rgba(0, 0, 0, 0);
  z-index: 999;
  word-break: break-all;
  resize: none;
}
.show-text {
  -webkit-text-fill-color: transparent;
}
</style>

<style>
.vin-matching-error {
  color: #ff4d4f;
  position: relative;
  z-index: 10000;
  word-break: break-all;
  font-size: 14px;
  line-height: 1.5715;
}
</style>

<template>
  <div style="padding: 50px">
    <a-row>
      <a-col :span="8">
        <div class="main">
          <div
            v-html="content"
            class="highlight-shadow-input common-style"
            spellcheck="false"
          ></div>
          <a-textarea
            v-model:value="vinStringq"
            cols="30"
            rows="10"
            class="highlight-input common-style"
            :class="{ 'show-text': vinStringq }"
            @input="processInput"
            @scroll="syncScroll"
            spellcheck="false"
            placeholder="请输入符合要求的数据。"
          ></a-textarea>
        </div>
      </a-col>
    </a-row>
  </div>
</template>

<script setup>
import { ref } from "vue";
const vinStringq = ref("");
const content = ref("");

let syncScroll = (event) => {
  let contentDivDom = document.querySelector(".highlight-shadow-input");
  const scrollTop = event.target.scrollTop;
  contentDivDom.scrollTop = scrollTop;
};

function processInput(e) {
  const keyWords = ["react", "vue","香蕉","苹果"];
  let value = e.target.value;
  keyWords.forEach((word) => {
    value = value
      .replaceAll(word, `<span style="color:#e52e2e">${word}</span>`)
      .replaceAll("\n", "<br>");
  });
  content.value = value;
}
</script>

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现Vue 中的搜索框中输入关键字后,将匹配到的内容高亮显示,可以遵循以下步骤: 1. 在 data 中定义一个变量来保存搜索关键字。 2. 在模板中使用 input 元素来渲染搜索框,并为其绑定一个 input 事件来监听输入内容变化。 3. 在模板中渲染需要搜索的内容,并使用 v-html 指令将内容渲染为 HTML。 4. 使用 computed 属性来实现关键字高亮的逻辑。在 computed 中定义一个方法,接收需要搜索的内容和搜索关键字,并使用正则表达式将匹配到的内容用 <span> 标签包裹,从而达到高亮的效果。 以下是一个示例代码: ``` <template> <div> <input type="text" v-model="searchText" @input="highlightText" placeholder="请输入搜索关键字"> <div v-html="highlightedContent"></div> </div> </template> <script> export default { data() { return { searchText: '', content: '这是一段需要搜索的内容,其中包含关键字 Vue高亮。' } }, computed: { highlightedContent() { const regex = new RegExp(this.searchText, 'gi') return this.content.replace(regex, '<span class="highlight">$&</span>') } }, methods: { highlightText() { // 触发 computed 中的方法 } } } </script> <style> .highlight { color: red; font-weight: bold; } </style> ``` 在这个示例中,我们在 data 中定义了 searchText 和 content 两个变量,其中 content 是需要搜索的内容。在模板中使用 input 元素来渲染搜索框,并使用 v-model 将输入的内容绑定到 searchText 上,在 input 事件中触发 highlightText 方法。使用 v-html 指令将 computed 中返回的内容渲染为 HTML。在 computed 中定义了一个方法 highlightedContent,使用正则表达式将匹配到的内容用 span 标签包裹。最后在样式中定义了高亮的样式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值