当前位置:网站首页>Wechat applet rich text picture width height adaptive method introduction (rich text)
Wechat applet rich text picture width height adaptive method introduction (rich text)
2022-06-24 10:24:00 【Wandering in memory of Yu Fei】
Wechat applet rich-text The method of adaptive width and height of rich text pictures
Invalid code
/* The following code does not work as expected ... */
rich-text img {
width: 100%;
object-fit: contain;
}
rich-text image {
width: 100%;
object-fit: contain;
}
encapsulation util.js
/** * Adaptive processing of image width in rich text * 1. Get rid of img Inside the label style、width、height attribute * 2.img add style attribute :max-width:100%;height:auto * 3. Modify all style Inside width The attribute is max-width:100% * 4. Get rid of <br/> label * @param html * @returns {void|string|*} */
function formatRichText(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, '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:block;margin-top:0;margin-bottom:0;"');
return newContent;
}
module.exports = {
formatRichText
}
Put rich text directly html Fields can be processed in this way ( Equals string substitution , Added width to the picture )
/* data.des Fields are rich text fields , Through the above method , to des Add width to the picture label in */
wx.request({
url: urlList.searchLouyu + `?id=${
id}`,
method: "GET",
success(res) {
if (res.data.msg == 'success') {
let data = res.data.list[0]
data.des = util.formatRichText(data.des)
that.setData({
listdata: data,
})
}
}
})
边栏推荐
猜你喜欢
随机推荐
2.登陆退出功能开发
为什么 JSX 语法这么香?
SQL Server AVG函数取整问题
uniapp 开发微信公众号,下拉框默认选中列表第一个
How large and medium-sized enterprises build their own monitoring system
SQL sever试题求最晚入职日期
[input method] so far, there are so many Chinese character input methods!
保健品一物一码防窜货营销软件开发
Groovy obtains Jenkins credentials through withcredentials
Floating point notation (summarized from cs61c and CMU CSAPP)
Graffiti smart brings a variety of heavy smart lighting solutions to the 2022 American International Lighting Exhibition
Leetcode-223: rectangular area
How to customize sharing links in wechat for H5 web pages
Engine localization adaptation & Reconstruction notes
Three ways to use applicationcontextinitializer
2022全网最全最细的jmeter接口测试教程以及接口测试流程详解— JMeter测试计划元件(线程<用户>)
SQL Sever关于like操作符(包括字段数据自动填充空格问题)
416 binary tree (first, middle and last order traversal iteration method)
6.套餐管理业务开发
24. 图像拼接大作业









