)中,再通过 v-html 渲…">
当前位置:首页 > VUE

vue实现搜索高亮

2026-03-09 07:08:23VUE

Vue 实现搜索高亮的方法

使用 v-html 和字符串替换

通过字符串替换将匹配的搜索词包裹在高亮标签(如 <span class="highlight">)中,再通过 v-html 渲染。

vue实现搜索高亮

<template>
  <div>
    <input v-model="searchText" placeholder="输入搜索词">
    <div v-html="highlightedContent"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      content: "这里是需要高亮显示的文本内容",
      searchText: ""
    };
  },
  computed: {
    highlightedContent() {
      if (!this.searchText) return this.content;
      const regex = new RegExp(this.searchText, "gi");
      return this.content.replace(regex, match => 
        `<span class="highlight">${match}</span>`
      );
    }
  }
};
</script>

<style>
.highlight {
  background-color: yellow;
  font-weight: bold;
}
</style>

使用自定义指令

通过自定义指令 v-highlight 动态处理文本节点的高亮逻辑。

vue实现搜索高亮

<template>
  <div>
    <input v-model="searchText" placeholder="输入搜索词">
    <div v-highlight="searchText">{{ content }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      content: "这里是需要高亮显示的文本内容",
      searchText: ""
    };
  },
  directives: {
    highlight: {
      inserted(el, binding) {
        const text = el.textContent;
        if (!binding.value) return;
        const regex = new RegExp(binding.value, "gi");
        el.innerHTML = text.replace(regex, match => 
          `<span class="highlight">${match}</span>`
        );
      },
      update(el, binding) {
        const text = el.textContent;
        if (!binding.value) {
          el.innerHTML = text;
          return;
        }
        const regex = new RegExp(binding.value, "gi");
        el.innerHTML = text.replace(regex, match => 
          `<span class="highlight">${match}</span>`
        );
      }
    }
  }
};
</script>

使用第三方库

借助第三方库如 mark.js 实现更复杂的高亮功能(如多词高亮、分词组匹配等)。

<template>
  <div>
    <input v-model="searchText" placeholder="输入搜索词">
    <div ref="content">{{ content }}</div>
  </div>
</template>

<script>
import Mark from "mark.js";
export default {
  data() {
    return {
      content: "这里是需要高亮显示的文本内容",
      searchText: ""
    };
  },
  watch: {
    searchText(newVal) {
      const markInstance = new Mark(this.$refs.content);
      markInstance.unmark();
      if (newVal) markInstance.mark(newVal);
    }
  }
};
</script>

动态样式与性能优化

对于长文本或频繁更新的场景,可以通过以下方式优化性能:

  • 使用 debounce 延迟搜索触发。
  • 避免直接操作 DOM,优先使用虚拟滚动(如 vue-virtual-scroller)。
<template>
  <div>
    <input 
      v-model="searchText" 
      placeholder="输入搜索词"
      @input="debouncedHighlight"
    >
    <div v-html="highlightedContent"></div>
  </div>
</template>

<script>
import { debounce } from "lodash";
export default {
  data() {
    return {
      content: "长文本内容...",
      searchText: "",
      debouncedHighlight: debounce(function() {}, 300)
    };
  },
  methods: {
    highlight() {
      // 高亮逻辑
    }
  },
  created() {
    this.debouncedHighlight = debounce(this.highlight, 300);
  }
};
</script>

注意事项

  • XSS 防护:使用 v-html 时需确保内容可信,避免注入攻击。
  • 大小写敏感:通过正则表达式参数(如 gi)控制是否忽略大小写。
  • 多词高亮:可通过拆分搜索词为数组,循环处理匹配逻辑。

标签: vue高亮
分享给朋友:

相关文章

vue实现导航切换内容

vue实现导航切换内容

Vue实现导航切换内容的方法 在Vue中实现导航切换内容,可以通过以下方式实现: 使用动态组件 动态组件通过<component :is="currentComponent">实现内容切…

vue 实现挂载

vue 实现挂载

Vue 实现挂载的基本方法 在 Vue 中,挂载是将 Vue 实例与 DOM 元素关联的过程。通过挂载,Vue 可以管理 DOM 并实现数据绑定和响应式更新。 创建 Vue 实例并挂载 const…

vue实现模块

vue实现模块

Vue 实现模块化的方法 Vue 支持多种模块化开发方式,可以根据项目需求选择适合的方案。 使用单文件组件(SFC) 单文件组件是 Vue 最常用的模块化方式,将模板、脚本和样式封装在一个 .vue…

vue实现水印

vue实现水印

Vue 实现水印的方法 使用 CSS 背景图 通过 CSS 的 background-image 和 background-repeat 属性实现水印效果。创建一个包含水印文本的 Canvas,将其转…

vue实现波形

vue实现波形

实现波形效果的方法 在Vue中实现波形效果可以通过多种方式完成,常见的方法包括使用Canvas绘制、CSS动画或第三方库。以下是几种实现方案: 使用Canvas绘制波形 通过Canvas API动态…

vue实现单击

vue实现单击

Vue 实现单击事件的方法 在 Vue 中实现单击事件可以通过 v-on 指令或简写的 @ 符号绑定事件处理器。以下是几种常见的实现方式: 基础用法 通过 v-on:click 或 @click 绑…