小程序rich-text接收富文本不起作用

后臺傳來的富文本有些并不能被rich-text識別以及良好的展現(xiàn),比如圖片大小問題,我們后臺傳來的含有很多<u>標(biāo)簽,盡管官方文檔顯示小程序支持,但實(shí)踐中并不可以,只好通過方法將其刪除,還有<被轉(zhuǎn)義成&lt這些。我們需要將其換成普通字符供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ù)進(jìn)行轉(zhuǎn)義,變成rich-text認(rèn)識的樣子
        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>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容