富文本图片溢出问题解决方法

在写小程序项目的过程中,发现有时候后端返回来的富文本里边嵌套着图片,但是图片的大小很大,溢出了屏幕,所以我们得把图片调整成合适大小

//html
<template>
    <view>
        <rich-text :nodes="text"></rich-text>
        <rich-text :nodes="text"></rich-text>
    </view>
</template>
//data的数据,模拟接口返回来的数据(摘自某个接口)
data() {
    return {
       text:"<p><img src=\"http://kuajing.shs.broing.cn/uploads/20230221/f5be91bd0313e35b8f612907814ca349.jpg\" width=\"591\" height=\"538\" alt=\"\" style=\"width: 591px; height: 538px;\"/></p>"
     };
},

//方法


format(html) {
    var GRT = [
        // img 样式
       ['img', "max-width:100%;height:auto;"],
    ];
    for (let i = 0; i < GRT.length; i++) {
       html = html.replace(new RegExp('<' + GRT[i][0] + '>|<' + GRT[i][0] + ' (.*?)>', 'gi'), function(word) {
     // 分析 dom 上是否带有 style=""
     if (word.indexOf('style=') != -1) {
       var regIn = new RegExp('<' + GRT[i][0] + '(.*?)style="(.*?)"(.*?)(/?)>', 'gi');
       return word.replace(regIn, '<' + GRT[i][0] + '$1style="$2 ' + GRT[i][1] + '"$3$4>');
      } else {
        var regIn = new RegExp('<' + GRT[i][0] + '(.*?)(/?)>', 'gi');
        return word.replace(regIn, '<' + GRT[i][0] + '$1 style="' + GRT[i][1] + '$2">');
      }
    });
   }
     return html;
  }

最后效果如下:

富文本中文字超出显示不全解决方式

可以给rich-text添加该类名


.rich-txt{
    word-break: break-all; //允许在单词内换行
    white-space: pre-line; //合并空白符序列,保留换行符
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值