小程序通过rich-text实现字符串转化为html,即 <rich-text node="{{html}}"></rich-text>。如果html字符串中含有img标签,并且img不能自适应,应该怎样解决呢??
下面是解决方法
两种方式:
-
给img加个class,在app.wxss或page.wxss中指定样式。
-
给img加style
首先通过将数据转化为json字符串 var jsonDa = JSON.stringify(res.data.data.ques).replace(/<img/gi, "<img class='richImg'style='auto!important;height:auto!important;max-height:100%;100%;'"); var newResData = JSON.parse(jsonDa); console.log(newResData);//得到的数据含有img标签的都有richImg类 this.setData({ arrList:newResData }) app.wxss rich-text .richImg{ max- 100%; max-height: 100%; vertical-align: middle; height: auto!important; auto!important; }