实战技术简友广场

rich-text---组件,基础内容

2018-10-03  本文已影响0人  前端来入坑

元素节点:type = node

包含三个属性
name
attrs
children

文本节点:type = text

包含一个属性
text

代码说明
wxml

<view class="page-body">
  <view class="page-section">
    <view class="page-section-title">传入html字符串</view>
    <view class="rich-text-wrp">
      <rich-text nodes="{{html}}" bindtap="tap"></rich-text>
    </view>
  </view>

  <view class="page-section">
    <view class="page-section-title">传入节点列表</view>
    <view class="rich-text-wrp">
      <rich-text nodes="{{nodes}}" bindtap="tap"></rich-text>
    </view>
  </view>
</view>

wxss

rich-text {
  width: 700rpx;
  padding: 25rpx 0;
}

.rich-text-wrp {
  padding: 0 25rpx;
  background-color: #fff;
}

.page-section{
  width: 100%;
  margin-bottom: 60rpx;
}

.page-section:last-child{
  margin-bottom: 0;
}

.page-section-title{
  font-size: 28rpx;
  color: #999999;
  margin-bottom: 10rpx;
  padding-left: 30rpx;
  padding-right: 30rpx;
}

js

Page({
  data: {
    html: '<div class="div_class" style="line-height: 60px; color: red;">Hello&nbsp;World!</div>',
    nodes: [{
      name: 'div',
      attrs: {
        class: 'div_class',
        style: 'line-height: 60px; color: red;'
      },
      children: [{
        type: 'text',
        text: 'Hello&nbsp;World!'
      }]
    }]
  },
  tap() {
    console.log('tap')
  }
})

效果预览


image.png

控制台里面查看


image.png

分析,如果代码是这样,js里面多了一个attrs和children有什么影响

Page({
  data: {
    html: '<div class="div_class" style="line-height: 60px; color: red;">Hello&nbsp;World!</div>',
    nodes: [{
      name: 'div',
      attrs: {
        class: 'div_class',
        style: 'line-height: 60px; color: red;'
      }, 
      attrs: {
        class: 'div_class',
        style: 'line-height: 60px; color: green;'
      },
      children: [{
        type: 'text',
        text: 'Hello&nbsp;World!'
      }],
      children: [{
        type: 'text',
        text: 'new Hello&nbsp;World!'
      }]
    }]
  },
  tap() {
    console.log('tap')
  }
})

新的效果预览


image.png

Bug & Tip

官方文档链接:https://developers.weixin.qq.com/miniprogram/dev/component/rich-text.html

上一篇 下一篇

猜你喜欢

热点阅读