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;
		
	}
相关推荐
郑州光合科技余经理2 小时前
本地生活系统:多业务订单字段怎么分账本导出
java·开发语言·前端·数据库·uni-app·php·ai编程
EatFan2 天前
Java接入支付宝 JSAPI 支付保姆教程(二):流程讲解与前后端代码讲解
前端·spring boot·后端·微信小程序·小程序·uni-app
bug总结3 天前
uniapp vue3全局方法注册使用
前端·javascript·uni-app
大佐不会说日语~3 天前
Android 16 下 uni-app APP 提示“网络连接失败”的排障与修复
android·uni-app
2501_915106323 天前
苹果App Store上架费用及流程全面解析
android·ios·小程序·https·uni-app·iphone·webview
白远山3 天前
家政服务派单平台搭建实战指南:从需求分析到系统设计全流程解析
java·开发语言·架构·uni-app·需求分析
宸翰3 天前
解决uni-app 中 SVG 图片在 iOS 端显示模糊问题
前端·uni-app
走到天涯海角3 天前
uniApp蓝牙在手机上测试
智能手机·uni-app
bug总结4 天前
uniapp的easycom自动组件注册机制
uni-app
HF_code4 天前
uniapp-history路由深度指南
uni-app