iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >【uni-app】rich-text的使用
  • 220
分享到

【uni-app】rich-text的使用

uni-app微信小程序前端小程序 2023-08-31 22:08:01 220人浏览 泡泡鱼
摘要

rich-text的使用 rict-text可以支持部分html节点及属性 rict-text的属性如下: nodes 值为 HTML String 时,在组件内部将自动解析为节点列表,推荐直接使用

rich-text的使用

rict-text可以支持部分html节点及属性
rict-text的属性如下:
在这里插入图片描述
nodes 值为 HTML String 时,在组件内部将自动解析为节点列表,推荐直接使用 Array 类型避免内部转换导致的性能下降。App-nVue 和支付宝小程序不支持 HTML String 方式,仅支持直接使用节点列表即 Array 类型,如要使用 HTML String,则需自己将 HTML String 转化为 nodes 数组,可使用 html-parser (opens new window)转换。

例如:

<rich-text nodes="{{htmlSnip}}"></rich-text>htmlSnip: [{        name: 'div',        attrs: {            class: 'div-class',            style: 'line-height: 60px; color: red; text-align:center;'        },        children: [{            type: 'text',            text: 'Hello uni-app!'        }]    }],

有时候展现的图片也是没有样式的,导致图片会按照原始大小显示,超出界面框架。我们需要用正则将内容转义一下:
新建一个js文件 replaceImg.js:

function fORMatRichText(html){  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;  });  newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){      match = match.replace(/width:[^;]+;/gi, 'width:100%;').replace(/width:[^;]+;/gi, 'width:100%;');      return match;  });  newContent = newContent.replace(/<br[^>]*\/>/gi, '');  newContent = newContent.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;margin:10px 0;"');  return newContent;}// module.exports = {//   formatRichText// }module.exports = {  formatRichText: formatRichText}

在对应的界面调用:

var replace_img = require("../../../utils/replaceImg.js");//requestlet data_article = res.data.data[0];//这里是请求到的 html 内容var newsArticle = replace_img.formatRichText(data_article.content);that.setData({  art_content:newsArticle});

来源地址:https://blog.csdn.net/loyd3/article/details/127726045

--结束END--

本文标题: 【uni-app】rich-text的使用

本文链接: https://www.lsjlt.com/news/387214.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作