在微信小程序中我們需要使用rich-text組件來展示html內容,但獲取的內容很多圖片都沒有設置自適應,需要我們手工修改
/*正則去除img中的css樣式 */ function removeCss(content) { let reg=/(style|class)="[^"]+"/gi; let img=/<img[^>]+>/gi; let res; if(img.test(content)) { res = content.match(img); for(let i=0;i<res.length;i++){ console.log(res[i].replace(reg,"")); content=content.replace(res[i],res[i].replace(reg,"")); } } return content.replace(/\<img/gi, '<img class="richImg" '); }
在wxss中設置樣式
rich-text .richImg{ max-width: 100%; max-height: 100%; vertical-align: middle; height: auto!important; width: auto!important; }