在微信小程序中我们需要使用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- 100%; max-height: 100%; vertical-align: middle; height: auto!important; auto!important; }