当前位置:首页 > VUE

vue实现高亮

2026-02-10 01:07:19VUE

Vue 实现高亮的方法

在 Vue 中实现高亮功能可以通过多种方式实现,以下是一些常见的方法:

使用 v-html 指令

通过 v-html 指令可以动态渲染包含 HTML 标签的字符串,从而实现高亮效果。需要确保内容的安全性,避免 XSS 攻击。

vue实现高亮

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

<script>
export default {
  data() {
    return {
      text: 'This is a sample text to highlight',
      searchTerm: 'sample'
    };
  },
  computed: {
    highlightedText() {
      const regex = new RegExp(this.searchTerm, 'gi');
      return this.text.replace(regex, match => `<span class="highlight">${match}</span>`);
    }
  }
};
</script>

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

使用自定义指令

可以创建一个自定义指令来动态高亮匹配的文本。

vue实现高亮

<template>
  <div v-highlight="searchTerm">{{ text }}</div>
</template>

<script>
export default {
  data() {
    return {
      text: 'This is a sample text to highlight',
      searchTerm: 'sample'
    };
  },
  directives: {
    highlight: {
      inserted(el, binding) {
        const regex = new RegExp(binding.value, 'gi');
        el.innerHTML = el.textContent.replace(regex, match => `<span class="highlight">${match}</span>`);
      },
      update(el, binding) {
        const regex = new RegExp(binding.value, 'gi');
        el.innerHTML = el.textContent.replace(regex, match => `<span class="highlight">${match}</span>`);
      }
    }
  }
};
</script>

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

使用第三方库

可以使用第三方库如 mark.js 来实现更复杂的高亮功能。

<template>
  <div ref="content">{{ text }}</div>
</template>

<script>
import Mark from 'mark.js';

export default {
  data() {
    return {
      text: 'This is a sample text to highlight',
      searchTerm: 'sample'
    };
  },
  mounted() {
    this.highlightText();
  },
  watch: {
    searchTerm() {
      this.highlightText();
    }
  },
  methods: {
    highlightText() {
      const markInstance = new Mark(this.$refs.content);
      markInstance.unmark();
      markInstance.mark(this.searchTerm, {
        className: 'highlight'
      });
    }
  }
};
</script>

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

动态样式绑定

通过动态绑定样式类来实现高亮效果。

<template>
  <div>
    <span
      v-for="(word, index) in words"
      :key="index"
      :class="{ highlight: word === searchTerm }"
    >
      {{ word }}
    </span>
  </div>
</template>

<script>
export default {
  data() {
    return {
      text: 'This is a sample text to highlight',
      searchTerm: 'sample'
    };
  },
  computed: {
    words() {
      return this.text.split(' ');
    }
  }
};
</script>

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

注意事项

  • 使用 v-html 时需注意安全性,避免注入恶意代码。
  • 自定义指令适用于需要复用高亮逻辑的场景。
  • 第三方库如 mark.js 提供了更多高亮选项和灵活性。
  • 动态样式绑定适用于简单的文本高亮需求。

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

相关文章

vue搜索过后实现分页

vue搜索过后实现分页

Vue 实现搜索后分页功能 数据绑定与搜索逻辑 在 Vue 组件中定义必要的数据属性: data() { return { searchQuery: '', currentPag…

vue中登陆实现逻辑

vue中登陆实现逻辑

Vue 中登录实现逻辑 前端实现逻辑 表单验证 使用 Vue 的表单验证库(如 VeeValidate)或自定义验证规则,确保用户输入的账号和密码符合要求。例如,检查邮箱格式、密码长度等。 发送登录…

vue实现跑车

vue实现跑车

Vue 实现跑车动画效果 使用 Vue 结合 CSS 动画可以实现跑车在屏幕上移动的效果。以下是一个完整的实现方案: 创建 Vue 组件结构 <template> <div c…

vue实现portal

vue实现portal

Vue 实现 Portal 功能 Portal 是一种将子节点渲染到父组件 DOM 层级之外的 DOM 节点的技术,常用于实现模态框、弹出层等需要脱离当前组件层级的场景。Vue 可以通过多种方式实现…

vue实现选择

vue实现选择

Vue 实现选择功能的方法 在 Vue 中实现选择功能可以通过多种方式完成,以下介绍几种常见的实现方法。 使用 v-model 绑定单选 通过 v-model 可以轻松实现单选功能。以下是一个简单的…

vue 实现拖拉

vue 实现拖拉

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