uniapp里 rich-text 里的img图片如何控制最大宽度

uniapp里的富文本展示的时候,要控制里面的图片的最大宽度;

用下面的方法字符串替换,给img标签加个内联样式

html 复制代码
<rich-text :nodes="processedDetail(product.product_contents)"></rich-text>
javascript 复制代码
	//处理富文本里的图片宽度限制的样式
	processedDetail(html) {
			if(!html){
				return '';
			}
		// 处理方法1: 强制替换所有图片样式
		  html = html.replace(/<img[^>]*>/g, (imgTag) => {
			// 移除 width 和 height 属性
			imgTag = imgTag.replace(/(width|height)=["'][^"']*["']/g, '');
			
			// 处理 style 属性
			if (imgTag.includes('style=')) {
			  // 移除原有的 width/height 样式,添加 max-width
			  imgTag = imgTag.replace(/style=["']([^"']*)["']/, (match, style) => {
				style = style.replace(/(width|height|max-width)[^;]*;?/g, '');
				return `style="${style}max-width: 100% !important; height: auto !important; display: block;"`;
			  });
			} else {
			  // 没有 style 属性,直接添加
			  imgTag = imgTag.replace('<img', '<img style="max-width: 100% !important; height: auto !important; display: block;"');
			}
			
			return imgTag;
		  });
		  
		   return html;
		
	}
相关推荐
2501_915909066 小时前
iOS test 测试怎么做?功能、性能、兼容、稳定与安全五类测试指南
android·ios·小程序·https·uni-app·iphone·webview
游戏开发爱好者81 天前
App Store 上传 IPA 自动化,.p8 密钥认证与 CI/CD 接入实战
android·运维·ci/cd·小程序·uni-app·自动化·iphone
游戏开发爱好者81 天前
iOS 推送怎么配置,APNs 推送证书、设备库与群发
android·ios·小程序·https·uni-app·iphone·webview
AI_AGENT_DEV_AI2 天前
原生 APP 开发的开发优点
uni-app
JackieDYH2 天前
uniapp-通知滚动组件+轮播图组件封装-案例
uni-app·轮播图·通知
2501_915921433 天前
appuploader-cli 命令行上传 IPA 到 App Store Connect upload CI 集成
android·ci/cd·小程序·https·uni-app·iphone·webview
结网的兔子3 天前
【前端开发】UniApp 项目地图选型与Web-APP跨端迁移方案
前端·uni-app
2501_916007474 天前
Bundle ID 注册与管理 App ID 创建指南 命名规则 权限开关与删除注意事项
android·ios·小程序·https·uni-app·iphone·webview
PedroQue994 天前
uni-app路由插件化:解锁高效开发新姿势
前端·uni-app
2501_915909064 天前
iOS 证书创建与管理 类型限制 p12 密码与云备份实操
android·ios·小程序·https·uni-app·iphone·webview