当前位置:首页 > VUE

vue实现检索内容标记

2026-02-09 14:31:55VUE

Vue 实现检索内容标记的方法

在 Vue 中实现检索内容标记功能,可以通过以下步骤完成。核心思路是利用正则表达式匹配检索关键词,并使用 v-html 或自定义指令动态渲染高亮内容。

方法一:使用 v-html 和正则表达式

通过正则替换匹配的关键词,生成带高亮样式的 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>

注意事项:

  • 使用 v-html 时需注意 XSS 风险,确保内容来源可信。
  • 正则表达式需处理特殊字符(如 *, ?),可通过转义实现:
const escapedText = this.searchText.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const regex = new RegExp(escapedText, 'gi');

方法二:自定义指令实现高亮

通过自定义指令动态处理 DOM 节点,避免直接使用 v-html

<template>
  <div>
    <input v-model="searchText" placeholder="输入关键词" />
    <div v-highlight="searchText">{{ content }}</div>
  </div>
</template>

<script>
export default {
  directives: {
    highlight: {
      updated(el, binding) {
        const text = el.textContent;
        const searchText = binding.value;
        if (!searchText) {
          el.innerHTML = text;
          return;
        }
        const regex = new RegExp(searchText, 'gi');
        el.innerHTML = text.replace(
          regex,
          (match) => `<span style="background-color: yellow">${match}</span>`
        );
      },
    },
  },
  data() {
    return {
      content: "这是一段需要标记的文本内容,包含关键词和其他信息。",
      searchText: "",
    };
  },
};
</script>

优势:

  • 指令可复用,逻辑与组件解耦。
  • 避免直接操作 HTML,安全性更高。

方法三:使用第三方库(如 mark.js

对于复杂场景(如多关键词、跨元素标记),可使用专用库。

  1. 安装 mark.js

    npm install mark.js
  2. 在 Vue 中使用:

    
    <template>
    <div>
     <input v-model="searchText" placeholder="输入关键词" />
     <div ref="content">{{ content }}</div>
    </div>
    </template>
import Mark from 'mark.js'; export default { data() { return { content: "这是一段需要标记的文本内容,包含关键词和其他信息。", searchText: "", }; }, watch: { searchText(newVal) { const marker = new Mark(this.$refs.content); marker.unmark(); if (newVal) marker.mark(newVal); }, }, }; ```

特点:

  • 支持动态标记/取消标记。
  • 提供配置项(如高亮样式、精准匹配等)。

样式优化建议

为高亮内容添加 CSS 类,避免内联样式:

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

在替换逻辑中使用类名替代内联样式:

`<span class="highlight">${match}</span>`

vue实现检索内容标记

标签: 标记内容
分享给朋友:

相关文章

vue实现导航切换内容

vue实现导航切换内容

Vue 实现导航切换内容 在 Vue 中实现导航切换内容可以通过动态组件、路由或条件渲染等方式完成。以下是几种常见方法: 动态组件结合 v-if 或 v-show 通过 v-if 或 v-show…

vue实现内容轮播

vue实现内容轮播

使用 Vue 实现内容轮播 基础轮播实现 安装依赖(如使用第三方库) 若选择 vue-awesome-swiper,通过以下命令安装: npm install swiper vue-aweso…

vue实现内容切换

vue实现内容切换

Vue 实现内容切换的方法 在 Vue 中实现内容切换可以通过多种方式,以下是几种常见的方法: 使用 v-if 和 v-else 指令 通过条件渲染动态切换内容。v-if 和 v-else 会根据表…

vue实现内容左右滚动

vue实现内容左右滚动

实现内容左右滚动的方法 在Vue中实现内容左右滚动可以通过多种方式完成,以下是几种常见的方法: 使用CSS的overflow-x属性 通过设置CSS的overflow-x: auto或overflo…

vue实现复制指定内容

vue实现复制指定内容

实现复制功能的几种方法 使用Clipboard API 现代浏览器支持Clipboard API,可以直接操作剪贴板内容。在Vue中可以通过以下方式实现: methods: { async…

vue实现tabbar切换内容

vue实现tabbar切换内容

Vue 实现 TabBar 切换内容 在 Vue 中实现 TabBar 切换内容可以通过动态组件或条件渲染实现。以下是两种常见方法: 动态组件实现 使用 Vue 的 <component&g…