uniapp富文本rich-text

1. uniapp富文本rich-text

官方文档:https://uniapp.dcloud.net.cn/component/rich-text.html

1.1. 示例

1.1.1. richText.vue

javascript 复制代码
<template>
  <view >
    <view class="rich-layout" >
      <rich-text :nodes="richText"></rich-text>
    </view>
  </view>
</template>
<script>
import newsJson from "/data/newsJson.json";
import richTextHelper from "/helper/richTextHelper";
export default {
  data() {
    return {
      // nodes: [
      //
      // ],
      //richText: '<div style="text-align:center;"><img src="https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni@2x.png"/></div>'
      richText: ""
    }
  },
  onLoad() {
    let that = this;
    let text = newsJson.data.newsContent
    text= richTextHelper.formatRichText(text)
    const urlRegex = new RegExp(`172.14.18.37:3103`, 'gi');
    text= text.replace(urlRegex, `90.310.123.156:3103`);
    console.log(text)
    that.richText=text
  },
  methods: {},
}
</script>
<style>
.rich-layout{
  width: 100%;
  font-size: 16px;
}
</style>

1.1.2. richTextHelper.vue

javascript 复制代码
/**
 * 处理富文本里的图片宽度自适应
 * 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) {
    let newContent;
    // 去掉video标签里的style、width、height属性
    newContent = html.replace(/<video[^>]*>/gi,
        function (match, capture) {
            match = match.replace(/style="[^"]+"/gi, '')
                .replace(/width="[^"]+"/gi, '')
                .replace(/height="[^"]+"/gi, '');
            return match;
        });
    // 修改所有style里的width属性为max-width:100%
    newContent = newContent.replace(/style="[^"]+"/gi,
        function (match, capture) {
        match = match.replace(/width:[^;]+;/gi, 'max-width:100%;');
        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. 问题

1.2.1. 使用rich-text标签渲染的富文本的css样式问题

对富文本的图片css进行处理列如宽度设定为750rpx,和table表格添加border,目前添加的css没有生效。

解决方案

javascript 复制代码
//你得直接处理接口返回的原格式,可以用正则匹配,例如
content= content.replace(/\<img/gi, '<img style="max-width:100%;"');
相关推荐
游戏开发爱好者88 小时前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
2501_9151063210 小时前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_9151063210 小时前
使用 Sniffmaster TCP 抓包和 Wireshark 网络分析
网络协议·tcp/ip·ios·小程序·uni-app·wireshark·iphone
宠友信息11 小时前
2025社交+IM及时通讯社区APP仿小红书小程序
java·spring boot·小程序·uni-app·web app
“负拾捌”12 小时前
python + uniapp 结合腾讯云实现实时语音识别功能(WebSocket)
python·websocket·微信小程序·uni-app·大模型·腾讯云·语音识别
局外人LZ1 天前
Uniapp脚手架项目搭建,uniapp+vue3+uView pro+vite+pinia+sass
前端·uni-app·sass
2501_915918411 天前
在 iOS 环境下查看 App 详细信息与文件目录
android·ios·小程序·https·uni-app·iphone·webview
前端呆头鹅1 天前
Websocket使用方案详解(uniapp版)
websocket·网络协议·uni-app
浮桥1 天前
uniapp+h5 公众号实现分享海报绘制
uni-app·notepad++
2501_916007471 天前
没有 Mac 用户如何上架 App Store,IPA生成、证书与描述文件管理、跨平台上传
android·macos·ios·小程序·uni-app·iphone·webview