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 小时前
uniapp h5 app 小程序获取当前定位
小程序·uni-app
*小雪2 小时前
uniapp写H5授权登录及分享,返回到目标页面
开发语言·javascript·uni-app
一只程序熊2 小时前
uniapp uniim ios配置消息推送
uni-app
2501_915106325 小时前
iOS性能调优的系统化实践,从架构分层到多工具协同的全流程优化指南(开发者深度版)
android·ios·小程序·架构·uni-app·iphone·webview
TE-茶叶蛋18 小时前
Uniapp运行MuMu模拟器
uni-app
一人一程温一壶酒20 小时前
微信小程序uniapp开发附源码——图片加水印
微信小程序·uni-app·notepad++
一个假的前端男1 天前
uniapp 3端轮播
前端·javascript·uni-app
Memory沙漏1 天前
IOS如何免费申请开发者证书(uniapp开发)
ios·uni-app
集成显卡1 天前
AI取名大师 | uni-app + Wot UI 跟随设备自动切换明暗主题
人工智能·ui·uni-app·外观配色