小程序通過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='width:auto!important;height:auto!important;max-height:100%;width:100%;'"); 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; }