微信小程序 rich-text使用正则去除html中img标签中的css样式
在微信小程序中我们需要使用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;
}

浙公网安备 33010602011771号