后臺傳來的富文本有些并不能被rich-text
識別以及良好的展現(xiàn)照藻,比如圖片大小問題糯彬,我們后臺傳來的含有很多<u>標簽纽乱,盡管官方文檔顯示小程序支持芳绩,但實踐中并不可以掀亥,只好通過方法將其刪除,還有<被轉(zhuǎn)義成<這些妥色。我們需要將其換成普通字符供rich-text
識別:
Page({
//html轉(zhuǎn)義方法
escape2Html(str) {
var arrEntities = { 'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&', 'quot': '"' };
return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all, t) { return arrEntities[t]; }).replace(/<section/g, '<div').replace(/<img/gi, '<img style="max-width:100%;height:auto" ').replace(/<u>/g,'').replace(/<u style="">/g,'').replace(/<\/u>/g,'');
},
onLoad: function (options) {
var that=this;
//封裝好的request方法
api('接口地址',Data).then(res=>{
if(res.data.code==1) {
//將數(shù)據(jù)進行轉(zhuǎn)義搪花,變成rich-text認識的樣子
var introduce= that.escape2Html(res.data.data.introduce)
that.setData({
introduce:introduce,
})
}else {
wx.showModal({
title: '提示',
content: res.data.msg
})
}
})
})
})
wxml中:
<rich-text nodes="{{introduce}}"></rich-text>