好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

微信小程序 rich-text的使用方法

rich-text是一个新增的微信小程序插件,从基础库1.4.0开始,低版本需要做兼容处理

nodes属性可为Array和String类型,但推荐使用Array.由于String类型最终也会转为Array类型

nodes分为支持两种节点,分别为元素节点(type=node ,默认为元素节点)文本节点(type=text)

元素节点

name        标签名     String   是  支持部分受信任的HTML节点
attrs       属性       Object   否 支持部分受信任的属性,遵循Pascal命名法
children    子节点列表  Array    否   结构和nodes一致123
<!-- rich-text.wxml --><rich-text nodes="{{nodes}}" bindtap="tap"></rich-text><!--{{nodes}}其中的变量名与data中名字相同--><!--支持默认事件tap、touchstart、touchmove、touchcancel、touchend和longtap-->1234
// rich-text.jsPage({
  data: {
    nodes: [{
      name: 'div',
      attrs: {
        class: 'div_class',
        style: 'width : 100px; height : 100px; color: red;'
      },
      children: [{
        type: 'text',
        text: 'Hello&nbsp;World!'
      }]
    }]
  },  tap() {
    console.log('tap')
  }
})12345678910111213141516171819

如果页面中存在多个富文本,富文本中存在多个孩子,请看下例:

<!-- rich-text.wxml --><rich-text nodes="{{nodes}}"></rich-text><rich-text nodes="{{nodes1}}"></rich-text>123
// rich-text.jsPage({
  data: {
    nodes: [{
      name: 'div',
      attrs: {        class: 'div_class',
        style: 'width : 100px; height : 100px; color: red;'
      },
      children: [{        type: 'text',
        text: 'Hello&nbsp;World!'
      }]
    }],
    nodes1: [{
      name: 'p',
      attrs: {        class: 'p_class',
        style: 'text-align : center; color: green;'
      },
      children: [{        type: 'text',
        text: '我是p标签!!!'
      },{
        name: "span",
        attrs: {
          style: "color:red",          class: "span_class"
        },
        children: [{          type: "text",
          text: '我是span标签,哈哈哈哈'
        }]
      }]
    }]
  },
})12345678910111213141516171819202122232425262728293031323334353637

文本节点

text    文本      String       是      支持entities1
<!-- rich-text.wxml --><rich-text nodes="{{nodes}}"></rich-text>12
// rich-text.jsPage({  data: {    nodes: "我是文本节点,意外不?"
  },})123456

查看更多关于微信小程序 rich-text的使用方法的详细内容...

  阅读:13227次