)。…">
当前位置:首页 > VUE

vue实现搜索高亮显示

2026-01-22 13:32:19VUE

实现搜索高亮的基本思路

在Vue中实现搜索高亮的核心逻辑是通过正则表达式匹配关键词,并将匹配到的内容替换为带有高亮样式的HTML标签(如<span class="highlight">)。需要结合Vue的数据绑定和动态渲染特性。

定义高亮样式

在CSS中定义高亮样式,例如:

.highlight {
  background-color: yellow;
  color: black;
  font-weight: bold;
}

创建高亮方法

在Vue组件中定义一个方法,用于处理文本并返回高亮后的HTML字符串:

vue实现搜索高亮显示

methods: {
  highlightText(text, keyword) {
    if (!keyword) return text;
    const regex = new RegExp(keyword, 'gi');
    return text.replace(regex, match => `<span class="highlight">${match}</span>`);
  }
}

模板中使用高亮

在模板中通过v-html指令渲染高亮后的内容(注意防范XSS风险):

<template>
  <div>
    <input v-model="searchKeyword" placeholder="输入搜索关键词" />
    <div v-html="highlightText(originalText, searchKeyword)"></div>
  </div>
</template>

处理多关键词高亮

若需支持多关键词高亮,可扩展高亮方法:

vue实现搜索高亮显示

highlightText(text, keywords) {
  if (!keywords) return text;
  const keywordArray = keywords.split(' ');
  let result = text;
  keywordArray.forEach(keyword => {
    if (keyword) {
      const regex = new RegExp(keyword, 'gi');
      result = result.replace(regex, match => `<span class="highlight">${match}</span>`);
    }
  });
  return result;
}

安全性注意事项

使用v-html时需确保内容可信,避免XSS攻击。可通过DOMPurify等库过滤危险内容:

import DOMPurify from 'dompurify';
// ...
highlightText(text, keyword) {
  const highlighted = /* 高亮逻辑 */;
  return DOMPurify.sanitize(highlighted);
}

性能优化建议

对于大量文本的高亮操作,可通过以下方式优化:

  • 使用防抖(debounce)延迟高亮计算
  • 避免在每次输入时重新处理整个文本
  • 考虑虚拟滚动技术处理长列表

完整组件示例

<template>
  <div>
    <input v-model="searchKeyword" placeholder="输入搜索关键词" />
    <div v-html="safeHighlightedText"></div>
  </div>
</template>

<script>
import DOMPurify from 'dompurify';

export default {
  data() {
    return {
      originalText: '这是一段需要高亮显示的示例文本,包含多个关键词。',
      searchKeyword: ''
    };
  },
  computed: {
    safeHighlightedText() {
      return DOMPurify.sanitize(this.highlightText(this.originalText, this.searchKeyword));
    }
  },
  methods: {
    highlightText(text, keyword) {
      if (!keyword) return text;
      const regex = new RegExp(keyword, 'gi');
      return text.replace(regex, match => `<span class="highlight">${match}</span>`);
    }
  }
};
</script>

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

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

相关文章

vue状态管理怎么实现

vue状态管理怎么实现

Vue 状态管理实现方法 使用 Vuex(官方推荐) Vuex 是 Vue 的官方状态管理库,适合中大型应用。 安装 Vuex: npm install vuex --save 创建 Store 示…

eventbus vue实现

eventbus vue实现

EventBus 实现原理 EventBus 是一种发布/订阅模式,用于 Vue 组件间的通信。核心是通过一个中央事件总线管理事件监听和触发。 创建 EventBus 在 Vue 项目中创建一个独立…

vue实现删除

vue实现删除

Vue 实现删除功能的方法 在 Vue 中实现删除功能通常涉及以下几个步骤: 数据绑定与列表渲染 使用 v-for 指令渲染列表数据,并为每个项绑定唯一标识符(如 id)。例如: <ul&g…

vue实现token

vue实现token

Vue 中实现 Token 认证的方法 在 Vue 中实现 Token 认证通常涉及前端与后端的交互,包括 Token 的获取、存储、发送和刷新等操作。以下是具体实现步骤: 获取 Token 用户登…

vue实现翻译

vue实现翻译

Vue 实现翻译功能的方法 使用 i18n 插件实现多语言切换 安装 vue-i18n 插件: npm install vue-i18n 创建语言资源文件(如 en.json 和 zh.json):…

vue拖拽实现

vue拖拽实现

Vue 拖拽实现方法 使用 HTML5 原生拖拽 API HTML5 提供了原生拖拽 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 drop 事…