当前位置:网站首页>微信小程序rich-text图片宽高自适应的方法介绍(rich-text富文本)
微信小程序rich-text图片宽高自适应的方法介绍(rich-text富文本)
2022-06-24 09:40:00 【徊忆羽菲】
无效代码
/* 下面代码并不能达到预想效果... */
rich-text img {
width: 100%;
object-fit: contain;
}
rich-text image {
width: 100%;
object-fit: contain;
}
封装util.js
/** * 处理富文本里的图片宽度自适应 * 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|*} */
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
}
直接把富文本 html 字段用此方法处理一下即可(等于字符串替换,给图片加上了宽度)
/* data.des 字段是富文本字段,通过上面的额方法,给 des 中的图片标签加上宽度 */
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,
})
}
}
})
边栏推荐
- SVG+js拖拽滑块圆形进度条
- Development of anti fleeing marketing software for health products
- How to improve the efficiency of network infrastructure troubleshooting and bid farewell to data blackouts?
- El table Click to add row style
- 小程序学习之获取用户信息(getUserProfile and getUserInfo)
- Internet of things? Come and see Arduino on the cloud
- Practical analysis: implementation principle of APP scanning code landing (app+ detailed logic on the web side) with source code
- 新手怎么选择投资理财产品的等级?
- 记录一下MySql update会锁定哪些范围的数据
- 涂鸦智能携多款重磅智能照明解决方案,亮相2022美国国际照明展
猜你喜欢
随机推荐
Jcim | AI based protein structure prediction in drug discovery: impacts and challenges
操作符详解
Observer mode
[db2] sql0805n solution and thinking
Impdp leading schema message ora-31625 exception handling
JS singleton mode
Go language development environment setup +goland configuration under the latest Windows
二叉树第一部分
TP5 using post to receive array data times variable type error: solution to array error
Why is JSX syntax so popular?
Producer / consumer model
Nvisual digital infrastructure operation management software platform
Wechat applet learning to achieve list rendering and conditional rendering
Graffiti smart brings a variety of heavy smart lighting solutions to the 2022 American International Lighting Exhibition
Error reading CSV (TSV) file
SQL Server AVG函数取整问题
物联网?快来看 Arduino 上云啦
vim的使用
被困英西中学的师生安全和食物有保障
PHP encapsulates a file upload class (supports single file and multiple file uploads)








