微信小程序 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;
}

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM