小程序富文本图片大小问题

文章目录

概要

在小程序使用富文本或者在nuiapp(小程序的)使用富文本都会转为

javascript 复制代码
<rich-text nodes="<p class="p class">内容</p>"></rich-text>

如果是这种情况的话在css里面就使用不了穿透样式了

javascript 复制代码
/deep/{}

uniapp小程序情况

pc:小程序里面的v-html里面什么东西东不能有,包括注释,否则会报错

javascript 复制代码
<view class="dowm">
	<view class="" v-html="ggDetailForm.msgContent">
	</view>
</view>

用uniapp写小程序在里面会自动转为

javascript 复制代码
<rich-text nodes="<p class="p class">内容</p>"></rich-text>

解决方法及完整示例

使用正则来修改富文本图片样式

javascript 复制代码
	function formatRichText(html){
	    console.log(html);
	    let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){
	        match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
	        match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
	        match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
	        return match;
	    });
	    newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){
	        match = match
	        .replace(/<p>/gi, '<p class="p_class">')
	        .replace(/width:[^;]+;/gi, 'max-width:100%;')
	        .replace(/width:[^;]+;/gi, 'max-width:100%;');
	        return match;
	    });
	    newContent = newContent.replace(/<br[^>]*\/>/gi, "");
	    newContent = newContent.replace(/<a>/gi, '<a class="p_class "');
	    newContent = newContent.replace(/<li>/gi, '<li class="p_class "');
	    newContent = newContent.replace(/\<p/gi, '<p class="p_class "');
	    newContent = newContent.replace(/\<span/gi, '<span class="p_class "');
	    newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');
	    return newContent;
	}
	export default {
	
		data() {
			return {
				formatRichText:formatRichText,
				
			}
		},
		methods: {
			ggDetail(id) {
				let obj = {
					id: id,
				}
				let that = this
				uni.$u.http
					.get('接口', {
						params: obj,
					})
					.then(res => {
						if (res.success) {
							that.ggDetailForm = res.result
							if(that.ggDetailForm.content){
								that.ggDetailForm.content = that.formatRichText(that.ggDetailForm.content)
							}
							
							
						}
					})
			},


		},
		}
相关推荐
Ama_tor2 小时前
mini-program01の系统认识微信小程序开发
微信小程序·小程序
阿蒙Amon13 小时前
C#正则表达式全面详解:从基础到高级应用
开发语言·正则表达式·c#
一只一只妖13 小时前
uniapp小程序无感刷新token
前端·小程序·uni-app
编程毕设14 小时前
基于小程序的智能停车管理系统设计与开发
小程序
Kelaru16 小时前
本地Qwen中医问诊小程序系统开发
python·ai·小程序·flask·project
初晨未凉17 小时前
uniapp返回webview返回小程序并且跳转回webview
小程序·uni-app
Python智慧行囊20 小时前
微信小程序交互精髓:点击操作与状态管理实战
微信小程序·小程序·交互
秋难降1 天前
Python 知识点详解(二)
数据库·python·正则表达式
paopaokaka_luck2 天前
基于SpringBoot+Vue的非遗文化传承管理系统(websocket即时通讯、协同过滤算法、支付宝沙盒支付、可分享链接、功能量非常大)
java·数据库·vue.js·spring boot·后端·spring·小程序