uniapp:富文本回显

一、使用uniapp官方的标签

rich-text:

会出现图片无法显示的问题,可以用以下方法来过滤处理

html 复制代码
<rich-text :nodes="question.title | formatRichHtml"></rich-text>
javascript 复制代码
        formatRichHtml(html) {
            if (!html) {
                return html;
            }
            //控制小程序中图片大小
            let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
                // console.log(match.search(/style=/gi));
                if (match.search(/style=/gi) === -1) {
                    match = match.replace(/\<img/gi, '<img style=""');
                }
                return match;
            });
            newContent = newContent.replace(/style="/gi, '$& max-width:100% !important; ');
            newContent = newContent.replace(/<br[^>]*\/>/gi, '');
            newContent = newContent.replace(/background-color[\s:]+[^;]*;/gi, '');
            return newContent;
        }

二、使用 扩展插件

还有一些扩展的,比如音视频

mp-html:

官方文档:小程序富文本组件

组件属性

属性 类型 默认值 说明
container-style String 容器的样式(2.1.0+
content String 用于渲染的 html 字符串
copy-link Boolean true 是否允许外部链接被点击时自动复制
domain String 主域名(用于链接拼接)
error-img String 图片出错时的占位图链接
lazy-load Boolean false 是否开启图片懒加载
loading-img String 图片加载过程中的占位图链接
pause-video Boolean true 是否在播放一个视频时自动暂停其他视频
preview-img Boolean true 是否允许图片被点击时自动预览
scroll-table Boolean false 是否给每个表格添加一个滚动层使其能单独横向滚动
selectable Boolean false 是否开启文本长按复制
set-title Boolean true 是否将 title 标签的内容设置到页面标题
show-img-menu Boolean true 是否允许图片被长按时显示菜单
tag-style Object 设置标签的默认样式
use-anchor Boolean false 是否使用锚点链接

完~

相关推荐
2501_91510632几秒前
使用 Sniffmaster TCP 抓包和 Wireshark 网络分析
网络协议·tcp/ip·ios·小程序·uni-app·wireshark·iphone
宠友信息1 小时前
2025社交+IM及时通讯社区APP仿小红书小程序
java·spring boot·小程序·uni-app·web app
“负拾捌”2 小时前
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
wangjun51591 天前
uniapp uni.downloadFile 偶发性下载文件失败 无响应
uni-app
2501_915106322 天前
当 Perfdog 开始收费之后,我重新整理了一替代方案
android·ios·小程序·https·uni-app·iphone·webview