小程序:設置 rich-text中標簽的樣式


1. 場景

   在小程序開發過程中,請求回來的數據,有時候會帶有h5標簽,直接展示的話: 會在頁面上顯示出h5標簽

   方法:使用 <rich-text mode="{{info}}"> </rich-text>  解析h5標簽

   現象:解析出的圖片、文字等樣式需要設置

 

2. 方法

   (1) 使用正則給標簽加上類名

   (2) 在此類名中寫上自己想要的樣式

   (3) 如  

  • js中--使用正則加類名      其中:info是從請求回來的數據
      info = info
          .replace(/<p([\s\w"=\/\.:;]+)((?:(style="[^"]+")))/ig, '<p')
          .replace(/<p([\s\w"=\/\.:;]+)((?:(class="[^"]+")))/ig, '<p')
          .replace(/<p>/ig, '<p class="p_class">')

          .replace(/<img([\s\w"-=\/\.:;]+)((?:(height="[^"]+")))/ig, '<img$1')
          .replace(/<img([\s\w"-=\/\.:;]+)((?:(width="[^"]+")))/ig, '<img$1')
          .replace(/<img([\s\w"-=\/\.:;]+)((?:(style="[^"]+")))/ig, '<img$1')
          .replace(/<img([\s\w"-=\/\.:;]+)((?:(alt="[^"]+")))/ig, '<img$1')
          .replace(/<img([\s\w"-=\/\.:;]+)/ig, '<img$1 class="pho"')

this.setData({
info: info
})

    

  • less中--定義樣式表
  rich-text {
      width: 670rpx;
      display: block;
      font-size: 28rpx;
      color: #353535;
      letter-spacing: 1rpx;

      .p_class {
        line-height: 46rpx;
        text-indent: 60rpx;
        margin-bottom: 20rpx;
      }

      .pho {
        display: block;
        width: 670rpx !important;
        height: 330rpx;
        border-radius: 20rpx;
      }

    }

 


免責聲明!

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



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