小程序怎樣控制rich-text中的標簽自適應


小程序通過rich-text實現字符串轉化為html,即 <rich-text node="{{html}}"></rich-text>。如果html字符串中含有img標簽,並且img不能自適應,應該怎樣解決呢??
  下面是解決方法

兩種方式:

  1. 給img加個class,在app.wxss或page.wxss中指定樣式。

  2. 給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;
    }
    

      


免責聲明!

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



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