//做了字匹配和全匹配,备注区分
<span v-html="item.question">{{item.question}}</span>
this.changeList(res.data.arrList); //接口拿到的实时搜索返回的数据
changeList(arr){
return arr.map((item)=>{
item.question=this.formatListContent(item.question);
return item
})
},
formatListContent(val){//全匹配
let keyword=this.inpText;
if(val.indexOf(keyword)!==-1){
return val.replace(keyword,`<span style="color:#3386ed">${keyword}</span>`)
}
}
formatListContent(val){//字匹配
let keyword=this.inpText.replace(/\s+/g,"");
let str=val;
let valSon="";
const tempVal=str.replace(/<\/?.+?\/?>/g,"");
const arr= tempVal.split("");
for(let i=0;i<arr.length;i++){
for(var j=0;j<keyword.length;j++){
if(arr[i].includes(keyword[j])){
arr[i]=`<font color=“#5079FF”>${arr[i]}</font>`;
return
}
}
}
return arr.join("");
}
input实时搜索关键字高亮
最新推荐文章于 2021-12-30 11:05:46 发布