小程序搜索关键字标红

2021-01-05  本文已影响0人  Stonesy
<block wx:for="{{item.title}}" wx:key="*this"  wx:for-item="items">
            <text class="news_title" wx:if="{{items.key == true}}" style="color:red;display:inline;">{{items.str}}</text>               
            <text class="news_title" style="display:inline;" wx:else>{{items.str}}</text>
         </block> 
// 根据搜索字分割字符
  hilight_word: function (key, word) {
    let idx = word.indexOf(key),
      t = [];

    if (idx > -1) {
      if (idx == 0) {
        t = this.hilight_word(key, word.substr(key.length));
        t.unshift({
          key: true,
          str: key
        });
        return t;
      }

      if (idx > 0) {
        t = this.hilight_word(key, word.substr(idx));
        t.unshift({
          key: false,
          str: word.substring(0, idx)
        });
        return t;
      }
    }
    return [{
      key: false,
      str: word
    }];
  }
上一篇 下一篇

猜你喜欢

热点阅读