引言:在微信小程序里,比如商品展示页面的商品详情会有图片展示,PC端设置的商品详情是PC端的宽度,所以在小程序里图片会显示不全,这时就应该做相应的处理,使小程序里图片显示正确
思路
- 把图片的宽度改为手机屏幕对应的宽度
微信小程序需要知道的知识
- 需要知道微信小程序里有自己的宽度标准,单位为rpx;
- 针对所有不同尺寸的浏览器,微信小程序里规定屏幕宽为750rpx;
解决
WXML
WXS
data={artical:''}
async onLoad(){
const json = await api.getDetail();
if(json !== null){
this.artical = util.formatRichText(json.detail.description);
}
}
若artical里只有图片,并且图片没有设置style和宽度/高度
util.js
function formatRichText(html){
let newContent= html.replace(/
若artical里包含多种标签
util.js
function formatRichText(html){
let newContent= html.replace(/]*>/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(/
]*/>/gi, '');
newContent = newContent.replace(/
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



