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;" ');
相关推荐
半桔2 小时前
【前端小站】CSS 样式美学:从基础语法到界面精筑的实战宝典
前端·css·html
_OP_CHEN2 小时前
【前端开发之CSS】(一)初识 CSS:网页化妆术的终极指南,新手也能轻松拿捏页面美化!
前端·css·html·网页开发·样式表·界面美化
web打印社区7 小时前
web-print-pdf:突破浏览器限制,实现专业级Web静默打印
前端·javascript·vue.js·electron·html
Rysxt_10 小时前
UniApp获取安卓系统权限教程
android·uni-app
顾西爵霞11 小时前
个人学习主页搭建指南:从毛坯房到精装户型
学习·html
tao35566714 小时前
【用AI学前端】HTML-01-HTML 基础框架
前端·html
咖啡の猫14 小时前
微信小程序案例 - 自定义 tabBar
微信小程序·小程序·notepad++
咖啡の猫14 小时前
微信小程序全局数据共享
微信小程序·小程序
Kratzdisteln15 小时前
【MCM】mermaid
前端·javascript·html
桐溪漂流15 小时前
微信小程序cli脚本预览上传
微信小程序·小程序