uniapp小程序使用rich-text富文本图片溢出问题

参考https://blog.csdn.net/chenny_/article/details/115534622, 看了很多文章,就这个好使,所以记录一下

在common下新建relpaceImg.js

// 正则变量
var graceRichTextReg;
 
// 批量替换的样式 [ 根据项目需求自行设置 ]
var GRT = [
	// div 样式
	['div', "line-height:2em;"],
	// h1 样式
	['h1', "font-size:3em; line-height:1.5em;"],
	// h2 样式
	['h2', "font-size:2em; line-height:1.8em;"],
	// h3 样式
	['h3', "font-size:1.6em; line-height:2em;"],
	// h4 样式
	['h4', "font-size:1.2em; line-height:2em;"],
	// h5 样式
	['h5', "font-size:1em; line-height:2em;"],
	// h6 样式
	['h6', "font-size:0.9em; line-height:2em;"],
	// p 样式
	['p', "font-size:1em; line-height:2em;"],
	// b 样式
	['b', "font-size:1em; line-height:2em;"],
	// strong 样式
	['strong', "font-size:1em; line-height:2em;"],
	// code 样式
	['code', "font-size:1em; line-height:1.2em; background:#F6F7F8; padding:8px 2%; width:96%;"],
	// img 样式
	['img', "width:100%; margin:8px 0;"],
	// blockquote
	['blockquote', "font-size:1em; border-left:3px solid #D1D1D1; line-height:2em; border-radius:5px; background:#F6F7F8; padding:8px 2%;"],
	// li 样式
	['ul', "padding:5px 0; list-style:none; padding:0; margin:0;"],
	['li', "line-height:1.5em; padding:5px 0; list-style:none; padding:0; margin:0; margin-top:10px;"],
	// table
	['table', "width:100%; border-left:1px solid #F2F3F4; border-top:1px solid #F2F3F4;"],
	['th', "border-right:1px solid #F2F3F4; border-bottom:1px solid #F2F3F4;"],
	['td', "border-right:1px solid #F2F3F4; border-bottom:1px solid #F2F3F4; padding-left:5px;"]
];
 
 
module.exports = {
	format : function(html){
		html = html.replace(/<pre.*pre>?/gis, function(word){
			word =  word.replace(/[\n]/gi,'<br />');
			word =  word.replace(/    /gi,'<span style="padding-left:2em;"></span>');
			return word.replace(/[\t]/gi, '<span style="padding-left:2em;"></span>');
		});
		html = html.replace(/<pre/gi, '<p style="font-size:1em; margin:12px 0; line-height:1.2em; background:#F6F7F8; border-radius:5px; padding:8px 4%; width:92%;"');
		html = html.replace(/<\/pre/gi,"</p");
		for(let i = 0; i < GRT.length; i++){
			graceRichTextReg = new RegExp('<'+GRT[i][0]+'>|<'+GRT[i][0]+' (.*?)>', 'gi');
			html = html.replace(graceRichTextReg , function(word){
				// 分析 dom 上是否带有 style=""
				if(word.indexOf('style=') != -1){
					var regIn = new RegExp('<' + GRT[i][0] + '(.*?)style="(.*?)"(.*?)(/?)>', 'gi');
					return word.replace(regIn, '<'+ GRT[i][0] +'$1style="$2 ' + GRT[i][1] +'"$3$4>');
				}else{
					var regIn = new RegExp('<' + GRT[i][0] + '(.*?)(/?)>', 'gi');
					return word.replace(regIn, '<'+ GRT[i][0] +'$1 style="' + GRT[i][1] +'$2">');
				}
			});
		}
		return html;
	}
	
}

在使用的页面引用js

import replace_img from "@/common/replaceImg.js";

使用

this.newHTML = replace_img.format(oldHTML)
相关推荐
林涧泣1 小时前
【Uniapp-Vue3】Unicloud根据表结构将数据添加到数据库
uni-app
计算机-秋大田6 小时前
基于微信小程序的私家车位共享系统设计与实现(LW+源码+讲解)
java·vue.js·spring boot·后端·微信小程序·小程序·课程设计
禁默9 小时前
【学术投稿-2025年计算机视觉研究进展与应用国际学术会议 (ACVRA 2025)】从计算机基础到HTML开发:Web开发的第一步
前端·计算机视觉·html
虞书欣的612 小时前
Python小游戏29乒乓球
python·游戏·小程序·pygame
大模型铲屎官13 小时前
深入剖析 HTML5 新特性:语义化标签和表单控件完全指南
前端·html·编程·html5·语义化标签·表单控件
大美B端工场-B端系统美颜师15 小时前
站在JavaScript的视角去看,HTML的DOM和GLTF的Json数据。
javascript·html·json·gltf
大模型铲屎官19 小时前
前端表单验证终极指南:HTML5 内置验证 + JavaScript 自定义校验
前端·javascript·html·html5·表单验证·内置验证·自定义校验
林涧泣21 小时前
【Uniapp-Vue3】iconfont图标库的使用
前端·javascript·uni-app
ufosuai5551 天前
HTML基本语法
前端·html
说私域1 天前
基于开源AI智能名片2 + 1链动模式S2B2C商城小程序视角下的个人IP人设构建研究
人工智能·小程序·开源