关于uniapp:uniapp-在微信小程序中图片宽度显示问题

24次阅读

共计 1156 个字符,预计需要花费 3 分钟才能阅读完成。

在 uniapp 中,如果你的富文本图片显示宽度不失常,你能够通过设置图片的宽高属性来解决这个问题。例如,你能够在富文本中增加以下代码来设置图片的宽度为 100%:

<img src="your_image_url" style="width: 100%" /> 

另外,如果你想设置图片的高度,你能够增加以下代码:

<img src="your_image_url" style="height: 100px" /> 

当然,能够实现一个 filter 来主动对图片宽度进行解决。

export default {
    // ...
    filters: {formatRichHtml(html) {if (!html) {return html;}
            // 管制小程序中图片大小
            let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {console.log(match.search(/style=/gi));
                if (match.search(/style=/gi) === -1) {match = match.replace(/\<img/gi, '<img style=""');
                }
                return match;
            });
            newContent = newContent.replace(/style="/gi,'$& max-width:100% !important; ');
            newContent = newContent.replace(/<br[^>]*\/>/gi, '');
            return newContent;
        }
    }
    // ...
}

在调用时只须要如下调用

export default {
    // ...
    filters: {formatRichHtml(html) {if (!html) {return html;}
            // 管制小程序中图片大小
            let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {console.log(match.search(/style=/gi));
                if (match.search(/style=/gi) === -1) {match = match.replace(/\<img/gi, '<img style=""');
                }
                return match;
            });
            newContent = newContent.replace(/style="/gi,'$& max-width:100% !important; ');
            newContent = newContent.replace(/<br[^>]*\/>/gi, '');
            return newContent;
        }
    }
    // ...
}

在调用时只须要如下调用即可

<rich-text :nodes="xxxxxContent | formatRichHtml" />

正文完
 0