title: rich-text 富文本 header: develop nav: component sidebar: base_rich-text

webUrl: https://qft12m.smartapps.cn/component/rich-text/rich-text

解释: 富文本,nodes 属性推荐使用 Array 类型,由于组件会将 String 类型转换为 Array 类型,因而性能会有所下降。

属性说明

属性名 类型 默认值 必填 说明
nodes Array | String [] 节点列表 / HTML String
selectable Boolean false(基础库3.150.1以前版本)
true(基础库3.150.1及以后版本)
富文本是否可以长按选中,可用于复制,粘贴,长按搜索等场景。百度 APP 11.10 以上
name 标签名 String 支持部分受信任的HTML节点
attrs 属性 Object 支持部分受信任的属性,遵循Pascal命名法
children 子节点列表 Array 结构和nodes一致
text 文本 String 支持entities

nodes

现支持两种节点,通过type来区分,分别是 元素节点 和 文本节点 ,默认是元素节点,在富文本区域里显示的HTML节点。

说明
node 元素节点
text 文本节点

受信任的HTML节点属性说明

全局支持class和style属性,不支持id属性。

节点 属性
a
abbr -
b -
blockquote
br
code -
col span,width
colgroup span,width
dd
del
div -
dl -
dt -
em
fieldset -
h1 -
h2 -
h3 -
h4 -
h5 -
h6 -
hr
i -
img alt,src,height,width
ins -
label -
legend -
li
ol start,type
p
q -
span -
strong
sub -
sup -
table width
tbody -
td colspan,height,rowspan,width
tfoot
th colspan,height,rowspan,width
thead -
tr
ul -

示例

在开发者工具中预览效果

扫码体验

webUrl: https://qft12m.smartapps.cn/component/rich-text/rich-text - 图1 请使用百度APP扫码

代码示例 1: 通过HTML String渲染

:::codeTab

  1. <view class="wrap">
  2. <view class="card-area">
  3. <view class="top-description border-bottom">通过HTML String渲染</view>
  4. <view class="description">代码示例</view>
  5. <scroll-view scroll-y>
  6. <view class="cont border-bottom">{{htmlSnip}}</view>
  7. </scroll-view>
  8. <view class="description">渲染效果</view>
  9. <view class='rich-text'>
  10. <!-- 基础库 3.150.1 以前的版本,selectable 属性默认为 false,期望文本不可被选中时不用设置此属性 -->
  11. <rich-text nodes="{{htmlSnip}}"></rich-text>
  12. <!-- 基础库 3.150.1 及以后版本,selectable 属性默认为 true,期望文本不可被选中时需设置此属性为 false -->
  13. <!-- <rich-text selectable="false" nodes="{{htmlSnip}}"></rich-text> -->
  14. </view>
  15. </view>
  16. </view>
  1. const htmlSnip
  2. =`<div class="div_class">
  3. <h1>Title</h1>
  4. <p class="p">
  5. Life is&nbsp;<i>like</i>&nbsp;a box of
  6. <b>&nbsp;chocolates</b>
  7. </p>
  8. </div>`;
  9. Page({
  10. data: {
  11. htmlSnip
  12. }
  13. });

:::

代码示例 2: 通过节点渲染

:::codeTab

  1. <view class="wrap">
  2. <view class="card-area">
  3. <view class="top-description border-bottom">通过节点渲染</view>
  4. <view class="description">代码示例</view>
  5. <scroll-view scroll-y>
  6. <view class="cont border-bottom">{{nodeSnip}}</view>
  7. </scroll-view>
  8. <view class="description">渲染效果</view>
  9. <view class='rich-text'>
  10. <!-- 基础库 3.150.1 以前的版本,selectable 属性默认为 false,期望文本不可被选中时不用设置此属性 -->
  11. <rich-text nodes="{{nodes}}"></rich-text>
  12. <!-- 基础库 3.150.1 及以后版本,selectable 属性默认为 true,期望文本不可被选中时需设置此属性为 false -->
  13. <!-- <rich-text selectable="false" nodes="{{nodes}}"></rich-text> -->
  14. </view>
  15. </view>
  16. </view>
  1. const nodeSnip
  2. =`
  3. Page({
  4. data: {
  5. nodes: [{
  6. name: 'div',
  7. attrs: {
  8. class: 'div_class',
  9. style: 'line-height: 60px; color: #4F99FB;;'
  10. },
  11. children: [{
  12. type: 'text',
  13. text: 'You never know what you're gonna get.'
  14. }]
  15. }]
  16. }
  17. })`;
  18. Page({
  19. data: {
  20. nodeSnip,
  21. nodes: [{
  22. name: 'div',
  23. attrs: {
  24. class: 'div_class',
  25. style: 'line-height: 60px; color: #4F99FB;'
  26. },
  27. children: [{
  28. type: 'text',
  29. text: 'You never know what you\'re gonna get.'
  30. }]
  31. }]
  32. }
  33. });

:::

Bug & Tip

  • Tip:单击此处,查看将富文本字符串转成 json 格式的具体方法。
  • Tip:支持默认事件,包括:tap、touchstart、touchmove、touchcancel、touchend和longtap。
  • Tip:nodes 不推荐使用 String 类型,性能会有所下降。
  • Tip:rich-text 组件内屏蔽所有节点的事件。
  • Tip:attrs 属性不支持 id,支持 class。
  • Tip:name 属性大小写不敏感。
  • Tip:如果使用了不受信任的HTML节点,该节点及其所有子节点将会被移除。
  • Tip:img 标签仅支持网络图片。
  • Tip:如果在自定义组件中使用 rich-text 组件,那么仅自定义组件的 swan 样式对 rich-text 中的 class 生效。