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

文章目录

概要

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


		},
		}
相关推荐
yw00yw30 分钟前
小程序插件使用
java·小程序·apache
00后程序员张39 分钟前
iOS 应用上架常见问题与解决方案,多工具组合的实战经验
android·ios·小程序·https·uni-app·iphone·webview
weixin_lynhgworld12 小时前
从闲置到珍宝:旧物回收小程序系统重塑物品价值
小程序·旧物回收
钢铁男儿15 小时前
Python 正则表达式(正则表达式和Python 语言)
python·mysql·正则表达式
钢铁男儿15 小时前
Python 正则表达式实战:解析系统登录与进程信息
开发语言·python·正则表达式
2501_9160074716 小时前
iOS App 上架实战 从内测到应用商店发布的全周期流程解析
android·ios·小程序·https·uni-app·iphone·webview
小小怪下士_---_1 天前
uniapp开发微信小程序自定义导航栏
前端·vue.js·微信小程序·小程序·uni-app
拾心211 天前
【运维进阶】Linux 正则表达式
linux·运维·正则表达式
莲动渔舟1 天前
第4.3节:awk正则表达式详解-特殊字符
正则表达式·编程语言·awk
G_H_S_3_1 天前
【网络运维】Linux:正则表达式
linux·运维·网络·正则表达式