当前位置: 首页 > article >正文

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

文章目录

    • 概要
    • uniapp小程序情况
    • 解决方法及完整示例

概要

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

<rich-text nodes="<p class="p class">内容</p>></rich-text>

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

/deep/{}

uniapp小程序情况

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

<view class="dowm">
	<view class="" v-html="ggDetailForm.msgContent">
	</view>
</view>

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

<rich-text nodes="<p class="p class">内容</p>></rich-text>

解决方法及完整示例

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

	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)
							}
							
							
						}
					})
			},


		},
		}

http://www.kler.cn/news/134826.html

相关文章:

  • 大数据安全 测试
  • Google的检索工具栏如何使用?
  • Vue 实现 Hover 功能( mouseover 与 mouseenter 的区别)
  • NodeMCU ESP8266构建Web Server网页端控制设备
  • docker打包chatpdf(自写)
  • 使用ChatGPT进行数据分析案例——贷款数据分析
  • java:IDEA中的Scratches and Consoles
  • 【软件工程师从0到1】- Java面向对象基础 (知识汇总)
  • 034、test
  • Virtual安装centos后,xshell连接centos
  • 斯坦福机器学习 Lecture1 (机器学习,监督学习、回归问题、分类问题定义)
  • ospf路由选路及路由汇总
  • 论文阅读——RetNet
  • UI 自动化测试框架设计与 PageObject 改造!
  • 【brpc学习实战三】同步、异步、半同步原理
  • VB.net读写S50/F08IC卡,修改卡片密码控制位源码
  • 警惕.360勒索病毒,您需要知道的预防和恢复方法。
  • IPKISS Tutorials 3------绘制矩形版图
  • Docker 安装 Oracle Database 23c
  • 前端图片转成base64
  • 8年资深测试,自动化测试常见问题总结,惊险避坑...
  • Docker基础知识总结
  • 医院陪诊服务预约小程序的作用如何
  • 源启容器平台KubeGien 打造云原生转型的破浪之舰
  • [uni-app]记录APP端跳转页面自动滚动到底部的bug
  • hiredis/examples /example-libevent.c
  • 如何进行手动脱壳
  • Hive客户端hive与beeline的区别
  • VR智慧景区:VR赋能文旅产业,激活消费潜能
  • EtherCAT 伺服控制功能块实现