美文网首页
小程序搜索关键字标红

小程序搜索关键字标红

作者: Stonesy | 来源:发表于2021-01-05 10:05 被阅读0次
<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
    }];
  }

相关文章

网友评论

      本文标题:小程序搜索关键字标红

      本文链接:https://www.haomeiwen.com/subject/ybjioktx.html