当前位置:网站首页>微信小程序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,
})
}
}
})
边栏推荐
- Five heart matchmaker
- Nvisual digital infrastructure operation management software platform
- 2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。
- SQL-统计连续N天登陆的用户
- Three ways to use applicationcontextinitializer
- nVisual数字基础设施运营管理软件平台
- 机器学习——主成分分析(PCA)
- Oracle database expdp only exports tables
- SQL Server AVG函数取整问题
- Distributed | how to make "secret calls" with dble
猜你喜欢

LeetCode: 240. Search 2D matrix II

oracle池式连接请求超时问题排查步骤

How large and medium-sized enterprises build their own monitoring system

413 binary tree Foundation

Basic operations on binary tree

Arbre binaire partie 1

indexedDB本地存储,首页优化

ssh远程免密登录

Geogebra instance clock

About thinkphp5, use the model save() to update the data prompt method not exist:think\db\query- & gt; Error reporting solution
随机推荐
Tutorial (5.0) 08 Fortinet security architecture integration and fortixdr * fortiedr * Fortinet network security expert NSE 5
Producer / consumer model
小程序学习之获取用户信息(getUserProfile and getUserInfo)
js代理模式
Honeypot 2 hfish, ehoney
Record the range of data that MySQL update will lock
有关二叉树 的基本操作
YOLOv6:又快又准的目标检测框架开源啦
保健品一物一码防窜货营销软件开发
Getting user information for applet learning (getuserprofile and getUserInfo)
Indexeddb local storage, homepage optimization
Cookie encryption 4 RPC method determines cookie encryption
MySQL data advanced
生产者/消费者模型
SQL statistics of users logged in for N consecutive days
Engine localization adaptation & Reconstruction notes
Programming questions (continuously updated)
机器学习——感知机及K近邻
植物生长h5动画js特效
微信小程序学习之 实现列表渲染和条件渲染.