如何使用Vue结合HTML实现搜索查询内容的关键词高亮
搜索关键词高亮实现方案
- 全程无需npm安装、无需引入第三方库,完全适配现有Vue+HTML代码逻辑
- 先修复现有代码的语法错误:原
postList数组初始化存在括号未闭合、多余分号的问题,会导致脚本直接运行报错
核心实现逻辑
通过一个通用的文本处理方法,将匹配到的搜索关键词替换为带高亮样式的HTML片段,再通过v-html渲染到页面对应位置。
代码改造步骤
1. 补全Vue实例的methods方法
在现有Vue配置中添加高亮处理的通用方法:
methods: { highlightText(originText) { // 搜索框为空时直接返回原文本 if (!this.search.trim()) return originText // 转义正则特殊字符,避免输入符号导致匹配失效 const escapeReg = this.search.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') // 不区分大小写匹配关键词,替换为带高亮类的标签 const reg = new RegExp(`(${escapeReg})`, 'gi') return originText.replace(reg, '<span class="search-highlight">$1</span>') } }
2. 修正原JS代码的语法错误
修正后完整JS代码参考:
class Post { constructor(title, link, author, img, course, coursel, cours, coursl, cour, courl, cou, coul, co, col) { this.title = title; this.link = link; this.author = author; this.img = img; this.course = course; this.coursel = coursel; this.cours = cours; this.coursl = coursl; this.cour = cour; this.courl = courl; this.cou = cou; this.coul = coul; this.co = co; this.col = col; } } const app = new Vue({ el: "#app", data: { search: "", postList: [ // 原代码缺失数组闭合,补全参数末尾值 new Post("abc", null, "xyz", "wqw", "qwe", "", " ", null, " ", null, " ", null, " ", null, "") ] }, computed: { filteredList() { return this.postList.filter(post => { return post.title.toLowerCase().includes(this.search.toLowerCase()) || post.author.toLowerCase().includes(this.search.toLowerCase()) || post.course.toLowerCase().includes(this.search.toLowerCase()) || post.cours.toLowerCase().includes(this.search.toLowerCase()) || post.cour.toLowerCase().includes(this.search.toLowerCase()) || post.cou.toLowerCase().includes(this.search.toLowerCase()) || post.co.toLowerCase().includes(this.search.toLowerCase()); }); } }, methods: { highlightText(originText) { if (!this.search.trim()) return originText const escapeReg = this.search.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') const reg = new RegExp(`(${escapeReg})`, 'gi') return originText.replace(reg, '<span class="search-highlight">$1</span>') } } });
3. 修改HTML渲染结构
替换原有文本输出方式,添加高亮样式:
<!-- 高亮基础样式,可自行调整效果 --> <style> .search-highlight { background: #fff3cd; color: #d63384; font-weight: bold; padding: 0 2px; border-radius: 2px; } </style> <div id="app"> <div class="search-wrapper"> <input type="text" v-model="search" placeholder="Search" onfocus="if(this.value==this.defaultValue)this.value=''" onblur="if(this.value=='')this.value=this.defaultValue" /> </div> <div class="wrapper"> <div class="col-lg-12 col-md-4 card" v-for="post in filteredList"> <a v-bind:href="post.link" target="_blank"> <img v-bind:src="post.img"/> <div style="line-height:20%;"> <br> </div> <font size="2" v-html="highlightText(post.author)"></font> <div class="brmedium"></div> <font size="5"><b><i><em v-html="highlightText(post.title)"></em></i></b></font> <a v-bind:href="post.coursel" title="View Credential" target="_blank"><font size="3" v-html="highlightText(post.course)"></font></a> <a v-bind:href="post.coursl" title="View Credential" target="_blank"><font size="3" v-html="highlightText(post.cours)"></font></a> <a v-bind:href="post.courl" title="View Credential" target="_blank"><font size="3" v-html="highlightText(post.cour)"></font></a> <a v-bind:href="post.coul" title="View Credential" target="_blank"><font size="3" v-html="highlightText(post.cou)"></font></a> <a v-bind:href="post.col" title="View Credential" target="_blank"><font size="3" v-html="highlightText(post.co)"></font></a> </a> </div> </div> </div>
注意事项
- 所有参与搜索匹配的字段,都需要单独调用
highlightText方法处理,才能触发对应位置的高亮 - 由于渲染的是自有静态数据,不存在代码注入风险
- 所有代码无外部依赖,复制到现有项目即可运行
内容的提问来源于stack exchange,提问作者kartikkasat
相关产品推荐
相关产品推荐

