微信小程序rich-text富文本图片不展示、宽度超出

背景:

    本地做微信小程序项目练习,有关rich-text遇到了以下问题。

问题:

  1.   产品详情页中富文本编辑器中的图片无法展示? 

       解决:

<rich-text nodes="{{content}}"></rich-text>

   ps: content中的图片地址必须为网络地址,后台可做处理,如下

 $info['content'] = str_replace('/Uploads',"http://www.***.cn/Uploads",$info['content']);
2. 以上图可展示,但是图片宽度超出容器?

     解决:两方法选其一即可

     法一:接口中给img加标签(php)

$info['content'] = str_replace("<img ", "<img style='max-width:100%;height:auto;'", $info['content']);

   法二:微信小程序js文件中给img加标签

 that.setData({
          content: res.data.content.replace(/\<img/gi, '<img style="max-width:100%;height:auto"')
        })

或

 that.setData({
          content: res.data.contents.replace('<img ', '<img style="max-width:100%;height:auto"')
        })

即可。

猜你喜欢

转载自blog.csdn.net/qq_37301074/article/details/91412099