赞
踩
搜索出来的列表关键字高亮
首先循环搜索的字符串拿到所有的需要高亮的字符,再使用RegExp方法正则匹配所有相同字符,使用模板字符串给匹配到的字符加上高亮的样式,再使用replace方法将正则匹配到的字符替换为模板字符串处理后的高亮字符,返回处理后的数据,页面中用v-html渲染即可。
if (this.name) {
let str = this.name
res.rows.forEach(item=>{
for (var i = 0; i < str.length; i++) {
let replaceReg = new RegExp(str[i], "ig");
let replaceString =
`<span style="color: #348BFF; font-weight: 700; font-size: 28rpx">${str[i]}</span>`;
item.title = item.title.replace(replaceReg, replaceString);
}
})
}
<view v-html="item.title" class="publicTextFontSize font"></view>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。