最近在小程序的開發過程當中遇到這樣的需求,一個搜索聯想關鍵詞高亮,另外一個是將後臺傳過來的富文本解析成html展現在頁面中,這裏咱們引入很是牛X的
wxParse
,之因此牛,是由於能夠解決不少種問題,正由於如此因此它對於小程序來講也很是大,雖然只有100多kb。javascript
https://github.com/icindy/wxParsecss
單行文本解析
#### wxmlhtml
<import src="../../../utils/wxParse/wxParse.wxml"/> <template is="wxParse" data="{{wxParseData:article.nodes}}" />
#### wxssjava
@import '../../../utils/wxParse/wxParse.wxss';
#### javascriptnode
let WxParse = require('../../../utils/wxParse/wxParse.js'); Page({ data: { wxParseData: '' }, toHtml () { let article = '<div>123 <span>456</span></div>'; WxParse.wxParse('article', 'html', article, this, 5); } })
數組文本解析
#### wxmlgit
<import src="../../../utils/wxParse/wxParse.wxml"/> <view class='list_for list_item' wx:key="{{index}}" wx:for="{{replyTemArray}}"> <view class="list-item" data-index="{{index}}" bindtap='clickListItem'> <template class='list_item' is="wxParse" data="{{wxParseData:item}}" /> </view> </view>
#### wxssgithub
@import '../../../utils/wxParse/wxParse.wxss';
#### javascript小程序
let WxParse = require('../../../utils/wxParse/wxParse.js'); Page({ data: { replyTemArray: [] }, // 搜索聯想,關鍵詞高亮 keywordAssociation(keyword) { let _this = this; getSearchList(keyword).then(res => { _this.setData({ searchList: res }) let dataArr = res; for (let i = 0; i < dataArr.length; i++) { WxParse.wxParse('reply' + i, 'html', _this.highLight(dataArr[i].name, keyword), _this); if (i === dataArr.length - 1) { WxParse.wxParseTemArray("replyTemArray", 'reply', dataArr.length, _this) } } }) } })
我的博客:午後南雜數組