小程序怎样控制rich-text中的标签自适应?

小程序通过rich-text实现字符串转化为html,即 <rich-text node="{{html}}"></rich-text>。如果html字符串中含有img标签,并且img不能自适应,应该怎样解决呢??

  下面是解决方法

两种方式:

  1. 给img加个class,在app.wxss或page.wxss中指定样式。

  2. 给img加style

    首先通过将数据转化为json字符串
     var jsonDa = JSON.stringify(res.data.data.ques).replace(/<img/gi, "<img class='richImg'");
     var newResData = JSON.parse(jsonDa);
        console.log(newResData);//得到的数据含有img标签的都有richImg类
    this.setData({
        arrList:newResData 
    })
    
    
    
    app.wxss
    rich-text .richImg{
      max-width: 100%;
      max-height: 100%;
      vertical-align: middle;
      height: auto!important;
      width: auto!important;
    }