imgStyle(val){ if(val!=''&&val!=undefined&&val!=null){ return val?.replace(/
赞
踩
<!-- #ifndef MP-ALIPAY -->
<view class="detailsContent" v-html="imgStyle(details.text)">
</view>
<!-- #endif -->
imgStyle(val){
if(val!=''&&val!=undefined&&val!=null){
return val?.replace(/<img/g, '<img style="max-width: 100%!important;display: block;margin: 0 auto;"')
}else{
return val;
}
},
<!-- #ifdef MP-ALIPAY -->
<rich-text :nodes="changeText(details.text)"></rich-text>
<!-- #endif -->
/**支付宝处理富文本文件*/ changeText(html){ /** * 处理富文本里的图片宽度自适应 * 1.去掉img标签里的style、width、height属性 * 2.img标签添加style属性:max-width:100%;height:auto * 3.修改所有style里的width属性为max-width:100% * 4.去掉<br/>标签 * @param html * @returns {void|string|*} */ if(html!=''&&html!=undefined&&html!=null){ 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, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;'); return match; }); newContent = newContent.replace(/<br[^>]*\/>/gi, ''); newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;"'); //将 HTML String转化为 nodes 数组 const content = new HTMLParser(newContent.replace()?.trim()); return content; }else{ return html; } }
用到的类库 html-parser.js
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。