小程序文字高亮

1,封装方法

 

  hilight_word(str, keyword) {
    var that = this;
    const idx = str.indexOf(keyword);
    var t = [];
    if (idx !== -1) {
      if (idx == 0) {
        t = that.hilight_word(str.substr(keyword.length), keyword);
        t.unshift({ isKeyword: true, value: keyword });
        return t;
      } else if (idx > 0) {
        t = that.hilight_word(str.substr(idx), keyword);
        t.unshift({ isKeyword: false, value: str.substring(0, idx) });
        return t;
      }
    }
    return [{ isKeyword: false, value: str }];
  },

2,调用处理


  //获取数据内容并进行文字颜色处理
  searchTap: function () {
    var that = this;
    var teList = that.data.textList;
    for (var i = 0; i < teList.length; i++) {
      var arr = teList[i].cenList;

      for (var j = 0; j < arr.length; j++) {
        var text = arr[j].centent;
        var mList = arr[j].minList;
        if (text != "") {
          var cen = that.hilight_word(text, "弹个店");//此处才是调用
          teList[i].cenList[j].centent = cen
        }
        if (mList != undefined) {
          for (var k = 0; k < mList.length;k++){
            var micen = mList[k].mincen;
            var men = that.hilight_word(micen, "弹个店");//此处才是调用
            mList[k].mincen = men
          }
        }
      }

    }
    that.setData({
      listDataCopy: teList,
    })
  },

 

 

3.效果图

小程序文字高亮

小程序文字高亮

上一篇:微信小程序初识


下一篇:小程序基础知识梳理