小程序:设置 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;
      }

    }

 

posted @ 2019-06-20 13:26  点点点旺旺旺  阅读(15766)  评论(0编辑  收藏  举报