Blog
首页
文档
收藏
关于
  • 在线转换时间戳 (opens new window)
  • 在线压缩图片 (opens new window)
  • Float-Double转二进制 (opens new window)
  • 文件转Hex字符串 (opens new window)

HiuZing

🍑
首页
文档
收藏
关于
  • 在线转换时间戳 (opens new window)
  • 在线压缩图片 (opens new window)
  • Float-Double转二进制 (opens new window)
  • 文件转Hex字符串 (opens new window)
  • 前端面试题

  • JavaScript

  • Vue2

  • port

  • CSS

  • Node.js

  • JavaScript优化

  • uniapp

    • uniapp-上传图片
    • uniapp-支付(支付宝和微信)
    • uniapp-复制(设置系统剪贴板)
    • uniapp-rich-text无法处理video问题
      • uniapp-rich-text无法处理video问题
        • 解决办法
    • uniapp-生产和开发环境
    • uniapp-设置全局变量
    • uniapp-自定义导航栏
    • uniapp-跨域
    • uniapp-获取用户手机号
    • uniapp-打包发布
    • uniapp-蓝牙
  • Mini Program

  • TypeScript

  • 面向对象编程

  • UI组件

  • Plugin

  • Vue3

  • 性能优化

  • Axios

  • 状态管理

  • React

  • Mock

  • Icon

  • Template

  • 构建工具

  • 项目规范配置

  • Taro

  • SVG

  • React Native

  • 前端
  • uniapp
HiuZing
2022-08-22
目录

uniapp-rich-text无法处理video问题

# uniapp-rich-text无法处理video问题

uni-app 中 rich-text 的支持并不包含 video

# 解决办法

播放 video 时使用的是uParse 富文本解析

  1. 在项目中导入插件

  2. 在页面中导入插件

    import uParse from '@/components/u-parse/u-parse.vue';
    
    1
  3. 替换rich-text

    <!-- <rich-text :nodes="content"></rich-text> -->
    <u-parse :content="content" />
    
    1
    2

### 默认的vedio样式需要修改时,可以使用正则表达式替换

1. 新建 js 文件 : richTextUtil.js

   ```js
   /**
    * 处理富文本里的图片宽度自适应
    * 1.去掉img、video标签里的style、width、height属性
    * 2.修改所有style里的width属性为max-width:100%
    * 3.img、video标签添加style属性:max-width:100%;height:auto
    * 4.去掉<br/>标签
    * @param html
    * @return string
    */
   function formatRichText (html) {
   	// 去掉img标签里的style、width、height属性
   	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;
   	});
   	
   	// 去掉video标签里的style、width、height属性
   	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;
   	});
   	// 修改所有style里的width属性为max-width:100%
   	// 去掉所有style里的font-size属性
   	newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){
   		match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
   		match = match.replace(/font-size:[^;]+;/gi, '');
   		return match;
   	});
   	// 去掉<br/>标签
   	newContent = newContent.replace(/<br[^>]*\/>/gi, '');
   	// img标签添加style属性:max-width:100%;height:auto
   	newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin:0px auto;"');
   	// video标签添加style属性:max-width:100%;height:auto
   	newContent = newContent.replace(/\<video/gi, '<video style="max-width:100%;height:auto;display:block;margin:0px auto;"');
   	return newContent;
   }
   
   export default {
   	formatRichText,
   };
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  1. 引入js库

    import richTextUtil from '@/common/richTextUtil.js';
    
    1
  2. 调用方法

this.content = richTextUtil.formatRichText(res.content);
1
上次更新: 2024/08/14, 04:14:33
uniapp-复制(设置系统剪贴板)
uniapp-生产和开发环境

← uniapp-复制(设置系统剪贴板) uniapp-生产和开发环境→

最近更新
01
React Native 使用SVG
08-13
02
Docker基础命令
08-04
03
算数逻辑单元
07-30
更多文章>
Theme by Vdoing | Copyright © 2021-2024 WeiXiaojing | 友情链接
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式