uniapp搜索关键字变色

https://blog.csdn.net/weixin_45936690/article/details/125263240?spm=1001.2014.3001.5501
这篇文章内容有bug,只匹配第一个符合的值,请使用此链接的方法,点击跳转。

请添加图片描述

<input class="input" type="text" placeholder="请输入机型搜索" placeholder-style="color: #DCDDE1" @input="ModelSearch" />

<view class="li" v-for="(item,index) in clone" :key="index">
  <image src="../../static/img/model/icon1.png" class="img"></image>
  <!-- <text v-html="item"></text> --> // H5
  <!-- <rich-text :nodes="item"></rich-text> --> // H5 兼容微信小程序

  // H5 兼容微信、支付宝小程序
  // mp-html 插件市场下载的插件
  <mp-html :content="item.name"/>
  <mp-html :content="item.str" class="text" />
</view>


// 输入框
ModelSearch(e) {
  let keyword = e.target.value; // 输入框的值
  let replaceReg = new RegExp(keyword, 'ig'); // 正则,不区分大小写
  let string = '<span style="background: white">'+ keyword +'</span>'; // 关键字样式
  this.clone = []; // 渲染数组
  for (let i=0; i<this.AlternativeModel.length; i++) {
    if (keyword && keyword.length > 0) {
      // this.clone.push(this.AlternativeModel[i].replace(replaceReg,string)); // H5 兼容微信小程序
      
      // 兼容H5 微信、支付宝小程序
      this.clone.push(
      {
        name: keyword,
        str: '<span style="background: #D7E4FF;">'+ this.AlternativeModel[i].split(replaceReg)[1] +'</span>',
      });
      
      console.log(this.clone);
    }
  }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值