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%;"');
相关推荐
2501_916007474 小时前
iOS和macOS应用程序性能分析和优化工具使用综合指南
android·macos·ios·小程序·uni-app·iphone·webview
2501_916007471 天前
深入理解HTTPS对称与非对称加密机制及Charles抓包实践
网络协议·http·ios·小程序·https·uni-app·iphone
小徐_23331 天前
AI 写 wot-ui 总在猜 API?我们把 Skills、MCP 和 CLI 都配好了
前端·uni-app·ai编程
Liu.7741 天前
uni-app 组件 uni-easyinput 常见 Bug 及解决方案
uni-app·bug
小徐_23332 天前
uni-app 项目别再从零搭了!3 个 Wot UI 起手模板怎么选?
前端·uni-app
蜡台2 天前
uniapp pdf文件预览组件
pdf·uni-app·合同·pdfh5
凡泰AI2 天前
如何借助MCP打通企业APP内部服务:从统一调用到小程序承接
小程序·uni-app·app·mpaas·mcp·小程序容器
华玥作者2 天前
uniapp 万条数据不卡顿:我写了个虚拟列表组件 hy-list,原生支持瀑布流
数据结构·uni-app·list·vue3
这是个栗子2 天前
uni-app 微信小程序开发:常用函数总结(一)
微信小程序·小程序·uni-app·getcurrentpages
宠友信息4 天前
消息序号如何保证即时通讯源码聊天记录稳定加载
java·spring boot·redis·python·mysql·uni-app