Skip to content

rich-text ​

富文本。

支持默认事件,包括:click、touchstart、touchmove、touchcancel、touchend、longpress。

属性说明

属性名类型默认值说明平台兼容
nodesArray / String[]节点列表 / HTML String
spacestring显示连续空格H5、微信基础库2.4.1+详见、抖音小程序、快手小程序详见
@itemclickEventHandle拦截点击事件(只支持 a、img标签),返回当前node信息 event.detail={node}H5
nodes ​

nodes 值为 HTML String 时,在组件内部将自动解析为节点列表,推荐直接使用 Array 类型避免内部转换导致的性能下降。

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

元素节点:type = node

属性说明类型必填备注
name标签名String是支持部分受信任的 HTML 节点
attrs属性Object否支持部分受信任的属性,遵循 Pascal 命名法
children子节点列表Array否结构和 nodes 一致

文本节点:type = text

属性说明类型必填备注
text文本String是支持 entities
受信任的HTML节点及属性 ​

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

节点属性
a
abbr
b
blockquote
br
code
colspan,width
colgroupspan,width
dd
del
div
dl
dt
em
fieldset
h1
h2
h3
h4
h5
h6
hr
i
imgalt,src,height,width
ins
label
legend
li
olstart,type
p
q
span
strong
sub
sup
tablewidth
tbody
tdcolspan,height,rowspan,width
tfoot
thcolspan,height,rowspan,width
thead
tr
ul 

示例 查看演示

以下示例代码,来自于hello uni-app项目,推荐使用HBuilderX,新建uni-app项目,选择hello uni-app模板,可直接体验完整示例。

vue
<template>
  <view class="content">
    <page-head :title="title"></page-head>
    <view class="uni-padding-wrap">
      <view class="uni-title uni-common-mt">
        数组类型
        <text>\nnodes属性为Array</text>
      </view>
      <view class="uni-common-mt" style="background:#FFF; padding:20rpx;">
        <rich-text :nodes="nodes"></rich-text>
      </view>
      <view class="uni-title uni-common-mt">
        字符串类型
        <text>\nnodes属性为String</text>
      </view>
      <view class="uni-common-mt" style="background:#FFF; padding:20rpx;">
        <rich-text :nodes="strings"></rich-text>
      </view>
    </view>
  </view>
</template>
<template>
  <view class="content">
    <page-head :title="title"></page-head>
    <view class="uni-padding-wrap">
      <view class="uni-title uni-common-mt">
        数组类型
        <text>\nnodes属性为Array</text>
      </view>
      <view class="uni-common-mt" style="background:#FFF; padding:20rpx;">
        <rich-text :nodes="nodes"></rich-text>
      </view>
      <view class="uni-title uni-common-mt">
        字符串类型
        <text>\nnodes属性为String</text>
      </view>
      <view class="uni-common-mt" style="background:#FFF; padding:20rpx;">
        <rich-text :nodes="strings"></rich-text>
      </view>
    </view>
  </view>
</template>
js
<script>
export default {
  data() {
    return {
      nodes: [{
        name: 'div',
        attrs: {
          class: 'div-class',
          style: 'line-height: 60px; color: red; text-align:center;'
        },
        children: [{
          type: 'text',
          text: 'Hello&nbsp;uni-app!'
        }]
      }],
      strings: '<div style="text-align:center;"><img src="https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni@2x.png"/></div>'
    }
  }
}
</script>
<script>
export default {
  data() {
    return {
      nodes: [{
        name: 'div',
        attrs: {
          class: 'div-class',
          style: 'line-height: 60px; color: red; text-align:center;'
        },
        children: [{
          type: 'text',
          text: 'Hello&nbsp;uni-app!'
        }]
      }],
      strings: '<div style="text-align:center;"><img src="https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni@2x.png"/></div>'
    }
  }
}
</script>

Tips

  • nodes 不推荐使用 String 类型,性能会有所下降。
  • rich-text 组件内屏蔽所有节点的事件。所以如果内容中有链接、图片需要点击,则不能使用rich-text,此时可在插件市场搜索parse插件使用。
  • attrs 属性不支持 id ,支持 class 。
  • name 属性大小写不敏感。
  • 如果使用了不受信任的HTML节点,该节点及其所有子节点将会被移除。
  • 小程序 img 标签仅支持网络图片。
  • 如果在自定义组件中使用 rich-text 组件,那么仅自定义组件的 css 样式对 rich-text 中的 class 生效。
  • 使用 itemclick 时,如果发生节点嵌套,外层 a标签 优先级高。