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

vue实现搜索高亮

2026-03-09 07:08:23VUE

Vue 实现搜索高亮的方法

使用 v-html 和字符串替换

通过字符串替换将匹配的搜索词包裹在高亮标签(如 <span class="highlight">)中,再通过 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 class="highlight">${match}</span>`
      );
    }
  }
};
</script>

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

使用自定义指令

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

<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>

动态样式与性能优化

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

vue实现搜索高亮

  • 使用 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中调用接口实现退出功能通常需要与后端API交互,以下是实现方法: 前端实现 创建退出方法,调用后端API接口: methods: { logout() {…

vue 实现复选

vue 实现复选

Vue 实现复选功能 在 Vue 中实现复选功能可以通过多种方式完成,以下是常见的几种方法: 使用 v-model 绑定数组 通过 v-model 绑定一个数组,可以自动收集用户选择的复选框值。…

vue实现datalist

vue实现datalist

使用 Vue 实现 HTML5 的 datalist 功能 HTML5 的 <datalist> 元素提供了一种自动完成输入框的功能,允许用户在输入时从预定义的选项列表中选择。以下是几种在…

vue实现链接

vue实现链接

Vue 实现链接的方法 在 Vue 中实现链接可以通过多种方式,包括使用 <router-link>、<a> 标签或编程式导航。以下是几种常见的方法: 使用 <rout…

vue 实现长按

vue 实现长按

实现长按功能的基本思路 在Vue中实现长按功能,通常需要监听元素的mousedown或touchstart事件,启动一个计时器。当用户按住元素超过设定的时间阈值时,触发长按回调函数。如果在时间阈值内触…

vue实现微云

vue实现微云

Vue 实现微云功能 使用 Vue 实现类似微云的网盘功能需要结合前端框架、后端存储及文件处理技术。以下是关键实现步骤和代码示例: 前端框架搭建 使用 Vue CLI 创建项目基础结构,安装必要依赖…