uniapp开发微信小程序遇到富文本内容大小变形问题v-html

uniapp开发微信小程序遇到富文本内容大小变形问题v-html

javascript 复制代码
<view class="description">
	<view>论坛介绍</view>
	<view class="html-content" v-html="forumDetailData.forumDescriptionCn"></view>
</view>
javascript 复制代码
this.forumDetailData.forumDescriptionCn = this.forumDetailData.forumDescriptionCn
	// 1. px 转 rem
	.replace(/(\d+)px/g, (match, p1) => {
		return (p1 * 0.04) + 'rem';
	})
	// 2. 给 id="companyBox" 添加 margin 样式
	.replace(/<(\w+)([^>]*?)id=["']companyBox["']([^>]*?)>/gi, (match, tag, beforeId,
		afterId) => {
		let fullAttr = beforeId + afterId;
		if (/style\s*=\s*["'].*?["']/.test(fullAttr)) {
			return match.replace(/style=["'](.*?)["']/i, (styleMatch,
				styleContent) => {
				return `style="${styleContent} margin: 1rem 0;"`;
			});
		} else {
			return `<${tag}${beforeId}id="companyBox"${afterId} style="margin: 1rem 0;">`;
		}
	})
	// 3. 背景图处理,追加 background-size: 80%
	.replace(/style=["'](.*?)["']/gi, (match, styleContent) => {
		if (/background(-image)?:\s*url\(['"]?.*?['"]?\)/.test(styleContent)) {
			if (/background-size\s*:/.test(styleContent)) {
				return match; // 已有 background-size
			} else {
				return `style="${styleContent} background-size: 80%;"`;
			}
		}
		return match;
	})
	// 4. 给 <img> 添加样式,防止样式失效
	.replace(/<img /g,
		'<img style="max-width:100%;height:auto;display:block;margin-bottom:10rpx;" ');
相关推荐
00后程序员张1 小时前
Charles中文版抓包工具功能解析,提升API调试与网络性能优化
android·ios·小程序·https·uni-app·iphone·webview
杨超越luckly4 小时前
HTML应用指南:利用GET请求获取全国Apple Store 零售店位置信息
大数据·前端·arcgis·html·数据可视化·门店
xkxnq12 小时前
微信小程序性能优化
微信小程序·小程序
2501_9160137413 小时前
iOS混淆工具有哪些?跨平台 App 混淆与保护的实用方案
android·ios·小程序·https·uni-app·iphone·webview
2501_9159090613 小时前
iOS 文件管理实战指南,用户文件、安全访问与开发调试方案
android·ios·小程序·https·uni-app·iphone·webview
小小黑00717 小时前
总结运行CRMEB标准版(uniapp)微信小程序的问题
微信小程序·小程序·uni-app
她说她一如既往的爱我18 小时前
uniapp 实现选择文件上传相册选择拍摄的upload
uni-app
小楓120121 小时前
後端開發技術教學(三) 表單提交、數據處理
前端·后端·html·php
轻语呢喃1 天前
Tailwind CSS:原子类名驱动的现代CSS框架
css·html