当前位置:首页 > VUE

vue实现搜索高亮

2026-03-29 18:45:25VUE

实现搜索高亮的方法

使用 v-html 和正则表达式

通过正则表达式匹配关键词并替换为高亮标签,结合 v-html 渲染结果。

<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 style="background-color: yellow">${match}</span>`
      );
    }
  }
};
</script>

使用自定义指令

封装高亮逻辑为自定义指令,增强复用性。

<template>
  <div>
    <input v-model="searchText" />
    <div v-highlight="searchText">{{ content }}</div>
  </div>
</template>

<script>
export default {
  directives: {
    highlight: {
      updated(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 style="background-color: yellow">${match}</span>`
        );
      }
    }
  },
  data() {
    return {
      content: '示例文本内容',
      searchText: ''
    };
  }
};
</script>

使用第三方库

借助 mark.js 实现更复杂的高亮效果。

<template>
  <div>
    <input v-model="searchText" />
    <div ref="content">需要高亮的文本内容</div>
  </div>
</template>

<script>
import Mark from 'mark.js';
export default {
  data() {
    return {
      searchText: ''
    };
  },
  watch: {
    searchText(newVal) {
      const markInstance = new Mark(this.$refs.content);
      markInstance.unmark();
      if (newVal) markInstance.mark(newVal);
    }
  }
};
</script>

动态样式类名

通过动态绑定类名实现高亮样式切换。

<template>
  <div>
    <input v-model="searchText" />
    <div :class="{ 'highlight': searchText }">{{ content }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      content: '动态样式类名示例',
      searchText: ''
    };
  }
};
</script>

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

高亮组件封装

将高亮逻辑抽象为独立组件,提高可维护性。

<template>
  <div>
    <input v-model="searchText" />
    <Highlight :text="content" :query="searchText" />
  </div>
</template>

<script>
import Highlight from './Highlight.vue';
export default {
  components: { Highlight },
  data() {
    return {
      content: '组件封装示例',
      searchText: ''
    };
  }
};
</script>

Highlight.vue 中实现核心逻辑:

vue实现搜索高亮

<template>
  <div v-html="highlightedText"></div>
</template>

<script>
export default {
  props: ['text', 'query'],
  computed: {
    highlightedText() {
      if (!this.query) return this.text;
      const regex = new RegExp(this.query, 'gi');
      return this.text.replace(regex, match => 
        `<span class="highlight">${match}</span>`
      );
    }
  }
};
</script>

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

注意事项

  • 使用 v-html 时需防范 XSS 攻击,确保内容可信。
  • 正则表达式需处理特殊字符,避免匹配错误。
  • 高亮样式可根据需求自定义颜色或动画效果。

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

相关文章

vue实现视频会议

vue实现视频会议

使用 Vue 实现视频会议 技术选型 Vue.js 作为前端框架,结合 WebRTC 技术实现实时音视频通信。常用的库包括: peerjs:简化 WebRTC 的点对点连接。 socket.io:用…

vue原型实现

vue原型实现

Vue 原型实现的关键点 Vue.js 的原型系统是其响应式机制的核心部分,通过原型链实现数据的动态绑定和依赖收集。以下是实现 Vue 原型的主要技术要点。 数据响应式原理 Vue 通过 Objec…

vue实现高亮

vue实现高亮

Vue 实现文本高亮的方法 在 Vue 中实现文本高亮可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-html 指令 通过 v-html 指令可以动态插入 HTML 内容,将需要高…

vue实现详情

vue实现详情

Vue 实现详情页的常见方法 使用动态路由 在 Vue Router 中配置动态路由参数,通过 $route.params 获取详情 ID。示例路由配置: { path: '/detail/:i…

vue 实现赋值

vue 实现赋值

Vue 实现赋值的方法 在 Vue 中,赋值操作通常涉及数据绑定和响应式更新。以下是几种常见的赋值方式: 直接赋值给 data 属性 在 Vue 实例的 data 对象中定义的属性可以直接通过 th…

vue实现循环

vue实现循环

Vue 实现循环的方法 在 Vue 中,可以通过 v-for 指令实现循环渲染列表或对象。以下是几种常见的用法: 遍历数组 使用 v-for 遍历数组时,可以同时获取当前项和索引: <ul&…