当前位置:   article > 正文

微信小程序富文本图片设置最大宽100%_小程序 富文本 图片宽度

小程序 富文本 图片宽度

方式一:在style中追加 max-width:100%;height:auto; ,此方法优点灵活,对于富文本中所有图片都起作用

  1. // 商品详情html正则追加max-width
  2. textImageConvert(content){
  3. return content.replace(/<(img).*?(\/>|<\/img>)/g, function (mats) {
  4. if (mats.indexOf('style') < 0) {
  5. return mats.replace(/<\s*img/, '<img style="max-width:100%;height:auto;"');
  6. } else {
  7. return mats.replace(/style=("|')/, 'style=$1max-width:100%;height:auto;')
  8. }
  9. });
  10. }

方式二:这种方式会替换之前设置的样式

  1. textImageConvert(content) {
  2. return content.replace(/<img[^>]*>/gi, function (match, capture) {
  3. return match.replace(/style\s*?=\s*?([‘"])[\s\S]*?\1/ig, 'style="max-width:100%;height:auto;"') // 替换style
  4. });
  5. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/460061
推荐阅读
相关标签
  

闽ICP备14008679号