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

文章目录

概要

在小程序使用富文本或者在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)
							}
							
							
						}
					})
			},


		},
		}
相关推荐
爷可是个天才5 小时前
uniapp小程序IOS端,uni.createInnerAudioContext()无声音
小程序·uni-app
白臻10 小时前
小程序 npm 支持
前端·小程序·npm
guanpinkeji12 小时前
剧本杀小程序:助力商家发展,提高游戏体验
游戏·小程序·游戏开发·小程序开发·剧本杀·剧本杀小程序
酷爱码13 小时前
新闻电影资讯类小程序模板源码
小程序
Conan_Ritchie14 小时前
微信小程序简历Demo
微信小程序·小程序
酷爱码16 小时前
旅游计划定制小程序网页模板源码
小程序·旅游
努力学习的木子1 天前
uniapp如何隐藏默认的页面头部导航栏,uniapp开发小程序如何隐藏默认的页面头部导航栏
前端·小程序·uni-app
guanpinkeji1 天前
家政小程序的开发,带动市场快速发展,提高家政服务质量
小程序·小程序开发·家政·家政小程序·家政服务小程序
Cc_Debugger1 天前
微信小程序消息通知(一次订阅)
微信小程序·小程序
栗豆包1 天前
【计算机毕业设计】020基于weixin小程序订餐系统
小程序·课程设计