uniapp使用v-html调用接口,富文本图片 视频自适应大小

前端获取到后台数据 不做处理 就会出现下面问题 图片 视频超出视图显示不全

请添加图片描述
处理

//info 是富文本
<view v-if='info' v-html='replaceWhite(info)'></view>

调用下面方法

replaceWhite(html) {
    // 处理富文本默认图片,视频大小
			let newContent = html.replace(/<video[^>]*>/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;
			}).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(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi,
					'max-width:100%;');
				return match;
			});
			newContent = newContent.replace(/<br[^>]*\/>/gi, '');
			newContent = newContent.replace(/\<video/gi,
				'<video style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;"').replace(/\<img/gi,
				'<img style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;"');
			return newContent;
		},

参考大佬

感觉文章好的话记得点个心心和关注和收藏,有错的地方麻烦指正一下,如果需要转载,请标明出处,多谢!!!

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-10 16:42:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-10 16:42:04       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-10 16:42:04       82 阅读
  4. Python语言-面向对象

    2023-12-10 16:42:04       91 阅读

热门阅读

  1. Vue笔记(二)基本语法

    2023-12-10 16:42:04       49 阅读
  2. 二、安全与风险管理—信息安全治理

    2023-12-10 16:42:04       71 阅读
  3. 信息学奥赛一本通2058

    2023-12-10 16:42:04       53 阅读
  4. ElasticSearch之线程池

    2023-12-10 16:42:04       64 阅读
  5. HIVE学习(hive基础)

    2023-12-10 16:42:04       60 阅读
  6. React富文本编辑器wangEditor

    2023-12-10 16:42:04       64 阅读
  7. 12.9每日一题(备战蓝桥杯循环结构)

    2023-12-10 16:42:04       41 阅读