当前位置:首页 > VUE

vue实现搜索高亮

2026-02-17 18:34:15VUE

实现搜索高亮的方法

在Vue中实现搜索高亮可以通过多种方式完成,以下是几种常见的实现方法:

使用v-html和正则表达式

通过正则表达式匹配搜索关键词,并用HTML标签包裹匹配到的内容,最后使用v-html渲染结果。

<template>
  <div>
    <input v-model="searchText" placeholder="输入搜索内容" />
    <div v-html="highlightedText"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      originalText: '这是一段需要高亮显示的文本内容',
      searchText: ''
    }
  },
  computed: {
    highlightedText() {
      if (!this.searchText) return this.originalText
      const regex = new RegExp(this.searchText, 'gi')
      return this.originalText.replace(regex, match => 
        `<span style="background-color: yellow">${match}</span>`
      )
    }
  }
}
</script>

使用自定义指令

vue实现搜索高亮

创建自定义指令来处理文本高亮,使代码更模块化和可复用。

Vue.directive('highlight', {
  inserted(el, binding) {
    const text = el.textContent
    const searchText = binding.value
    if (!searchText) return

    const regex = new RegExp(searchText, 'gi')
    el.innerHTML = text.replace(regex, match => 
      `<span style="background-color: yellow">${match}</span>`
    )
  },
  update(el, binding) {
    const text = el.textContent
    const searchText = binding.value
    const regex = new RegExp(searchText, 'gi')
    el.innerHTML = text.replace(regex, match => 
      `<span style="background-color: yellow">${match}</span>`
    )
  }
})

使用组件封装

vue实现搜索高亮

创建一个专门的高亮组件,接收原始文本和搜索词作为props。

<template>
  <span>
    <template v-for="(part, index) in parts">
      <span 
        v-if="part.highlight" 
        :key="index" 
        style="background-color: yellow"
      >
        {{ part.text }}
      </span>
      <span v-else :key="index">
        {{ part.text }}
      </span>
    </template>
  </span>
</template>

<script>
export default {
  props: {
    text: String,
    search: String
  },
  computed: {
    parts() {
      if (!this.search) return [{ text: this.text, highlight: false }]

      const regex = new RegExp(this.search, 'gi')
      const matches = [...this.text.matchAll(regex)]

      if (matches.length === 0) return [{ text: this.text, highlight: false }]

      let lastIndex = 0
      const parts = []

      matches.forEach(match => {
        const index = match.index
        if (index > lastIndex) {
          parts.push({
            text: this.text.substring(lastIndex, index),
            highlight: false
          })
        }

        parts.push({
          text: match[0],
          highlight: true
        })

        lastIndex = index + match[0].length
      })

      if (lastIndex < this.text.length) {
        parts.push({
          text: this.text.substring(lastIndex),
          highlight: false
        })
      }

      return parts
    }
  }
}
</script>

注意事项

使用v-html时需要注意XSS攻击风险,确保搜索内容来自可信来源或进行适当转义。

高亮实现应考虑性能问题,特别是处理大量文本时,可以使用防抖技术优化搜索输入处理。

对于复杂搜索需求,可能需要更高级的正则表达式或分词处理来改善匹配效果。

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

相关文章

vue实现Siri

vue实现Siri

Vue 实现 Siri 风格语音交互 实现类似 Siri 的语音交互功能需要结合语音识别、语音合成和前端交互逻辑。Vue 作为前端框架,可以很好地管理这些功能的交互状态。 语音识别集成 使用 Web…

vue实现addclass

vue实现addclass

Vue 实现动态添加 class 的方法 在 Vue 中动态添加 class 可以通过多种方式实现,以下是常见的几种方法: 使用对象语法 通过绑定一个对象到 :class,可以动态切换 class…

vue实现模糊

vue实现模糊

Vue实现模糊搜索的方法 在Vue中实现模糊搜索功能通常需要结合输入框和列表渲染,通过监听输入内容动态过滤数据。以下是几种常见实现方式: 使用计算属性实现 计算属性适合处理需要响应式更新的搜索逻辑:…

vue实现抽屉

vue实现抽屉

Vue 实现抽屉组件 抽屉组件是一种常见的 UI 模式,通常用于从屏幕边缘滑出内容。以下是几种实现抽屉的方法: 使用 Vue 原生实现 创建一个基本的抽屉组件,利用 Vue 的过渡和条件渲染功能。…

vue实现产品搜索

vue实现产品搜索

实现产品搜索功能 在Vue中实现产品搜索功能需要结合前端界面和后端数据处理。以下是实现的基本思路和代码示例: 数据准备 创建一个产品数据数组,包含需要搜索的产品信息: data() { ret…

vue实现扫码

vue实现扫码

Vue 实现扫码功能 使用 vue-qrcode-reader 库 安装 vue-qrcode-reader 库: npm install vue-qrcode-reader 在 Vue 组件中引入…