)包裹,最后通过v-htm…">
当前位置:首页 > VUE

vue实现搜索高亮显示

2026-02-23 04:30:23VUE

实现搜索高亮的基本思路

在Vue中实现搜索高亮的核心是通过正则表达式匹配关键词,并将匹配到的内容用HTML标签(如<span class="highlight">)包裹,最后通过v-html渲染处理后的文本。需注意安全性问题,避免XSS攻击。

方法一:使用计算属性处理高亮

定义一个计算属性,将原始文本中的关键词替换为高亮样式:

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

<script>
export default {
  data() {
    return {
      content: "这里是需要高亮显示的文本内容,包含Vue和JavaScript关键词。",
      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-html时需确保内容来源可信,避免XSS攻击。
  • 正则表达式中的gi表示全局匹配且忽略大小写。

方法二:封装高亮为全局指令

将高亮逻辑封装为Vue自定义指令,方便复用:

// main.js 或单独指令文件
Vue.directive('highlight', {
  bind(el, binding) {
    const text = el.textContent;
    const search = binding.value;
    if (!search) return;
    const regex = new RegExp(search, 'gi');
    el.innerHTML = text.replace(
      regex,
      match => `<span class="highlight">${match}</span>`
    );
  },
  update(el, binding) {
    // 搜索词变化时重新渲染
    const text = el.textContent;
    const search = binding.value;
    if (!search) {
      el.innerHTML = text;
      return;
    }
    const regex = new RegExp(search, 'gi');
    el.innerHTML = text.replace(
      regex,
      match => `<span class="highlight">${match}</span>`
    );
  }
});

// 组件中使用
<template>
  <div v-highlight="searchText">{{ content }}</div>
</template>

方法三:处理多关键词高亮

若需支持多个关键词高亮,可通过拆分搜索词并循环处理:

computed: {
  highlightedContent() {
    if (!this.searchText) return this.content;
    const keywords = this.searchText.split(' ').filter(k => k.trim());
    let result = this.content;
    keywords.forEach(keyword => {
      const regex = new RegExp(keyword, 'gi');
      result = result.replace(
        regex,
        match => `<span class="highlight">${match}</span>`
      );
    });
    return result;
  }
}

安全增强建议

若内容可能包含用户输入,建议使用DOMPurify库过滤HTML:

npm install dompurify
import DOMPurify from 'dompurify';
computed: {
  highlightedContent() {
    const rawHtml = this.replaceKeywords(); // 替换逻辑
    return DOMPurify.sanitize(rawHtml);
  }
}

性能优化

对于长文本或高频搜索,可通过防抖(debounce)减少计算次数:

vue实现搜索高亮显示

import { debounce } from 'lodash';
methods: {
  updateHighlight: debounce(function() {
    this.highlightedContent = this.calculateHighlight();
  }, 300)
}

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

相关文章

vue实现人脸识别比对

vue实现人脸识别比对

Vue 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…

vue实现边框

vue实现边框

Vue 实现边框的方法 在 Vue 中实现边框效果可以通过多种方式完成,包括内联样式、CSS 类绑定、动态样式以及使用第三方 UI 库。以下是几种常见的实现方法。 内联样式绑定 使用 Vue 的 :…

vue实现钟表

vue实现钟表

使用 Vue 实现动态钟表 以下是一个基于 Vue 3 的动态钟表实现方案,包含时分秒指针和数字时间显示。 模板部分 <template> <div class="cloc…

jq 实现 vue

jq 实现 vue

jq 实现 Vue 的核心功能 jq 是一个轻量级的 JavaScript 库,主要用于 DOM 操作和事件处理。虽然它不能完全替代 Vue,但可以通过一些技巧模拟 Vue 的核心功能,如数据绑定、事…

vue compile 实现

vue compile 实现

Vue 编译实现原理 Vue 的编译过程将模板字符串转换为渲染函数,主要分为解析、优化和代码生成三个阶段。 解析阶段(Parse) 将模板字符串转换为抽象语法树(AST)。Vue 使用正则表达式和有…

vue实现picker

vue实现picker

Vue 实现 Picker 组件的方法 在 Vue 中实现 Picker 组件可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 HTML 和 CSS 实现 通过 Vue 的模板和样式绑定,…