百度小程序插入图片自适应设置

先获取数据

 //请求数据
    news_id_data:function(id){
        var that =this
        swan.request({
            // url:'',
            url: 'http://localhost:8080/app/news',
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            data:{
                id:id
            },
            method:'post',
            dataType:"json",
            success:function(res){
                that.setData({
                  contentData:res.data,
                  //替换图片里面的style 相关的样式 
                  content:that.formatRichText(res.data.content)
                })
               
            }
        });
    },
//使用正则表达式替换
//图片自适应
    formatRichText:function (html){
        let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){
            match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
            match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
            match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
            return match;
        });
        newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){
            match = match.replace(/width:[^;]+;/gi, 'width:100%;').replace(/width:[^;]+;/gi, 'width:100%;');
            return match;
        });
        newContent = newContent.replace(/<br[^>]*\/>/gi, '');
        newContent = newContent.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;margin:10px 0;"');
        return newContent;
    },

使用rich-text插入富编辑器获取的html代码

 <rich-text class="content" nodes="{{content}}" image-menu-prevent="true"></rich-text>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq110917832

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值