当前位置:网站首页>微信小程序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,
})
}
}
})
边栏推荐
- 利用pandas读取SQL Sever数据表
- Five heart matchmaker
- Symbol. Iterator iterator
- 简单的价格表样式代码
- 413 binary tree Foundation
- 新手怎么选择投资理财产品的等级?
- Phpstrom code formatting settings
- Getting user information for applet learning (getuserprofile and getUserInfo)
- 411-栈和队列(20. 有效的括号、1047. 删除字符串中的所有相邻重复项、150. 逆波兰表达式求值、239. 滑动窗口最大值、347. 前 K 个高频元素)
- ByteDance Interviewer: talk about the principle of audio and video synchronization. Can audio and video be absolutely synchronized?
猜你喜欢

vim的使用

411-栈和队列(20. 有效的括号、1047. 删除字符串中的所有相邻重复项、150. 逆波兰表达式求值、239. 滑动窗口最大值、347. 前 K 个高频元素)

canvas无限扫描js特效代码

机器学习——主成分分析(PCA)

CVPR 2022 Oral | 英伟达提出自适应token的高效视觉Transformer网络A-ViT,不重要的token可以提前停止计算

Basic operations on binary tree

Wechat applet learning to achieve list rendering and conditional rendering

Floating point notation (summarized from cs61c and CMU CSAPP)

一群骷髅在飞canvas动画js特效

How to standardize data center infrastructure management process
随机推荐
port 22: Connection refused
Getting user information for applet learning (getuserprofile and getUserInfo)
Go language development environment setup +goland configuration under the latest Windows
Array seamless scrolling demo
买的长期理财产品,可以转短吗?
canvas掉落的小球重力js特效动画
新手怎么选择投资理财产品的等级?
How large and medium-sized enterprises build their own monitoring system
2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。
为什么 JSX 语法这么香?
引擎国产化适配&重构笔记
Engine localization adaptation & Reconstruction notes
2021-08-17
PHP uses recursive and non recursive methods to create multi-level folders
队列Queue
415 binary tree (144. preorder traversal of binary tree, 145. postorder traversal of binary tree, 94. inorder traversal of binary tree)
SVG+js拖拽滑块圆形进度条
PHP file lock
oracle池式连接请求超时问题排查步骤
ssh远程免密登录