微信小程序之富文本編輯組件editor結(jié)合RichText進(jìn)行顯示

image

核心代碼

var that;

Page({
  data: {
    content: '',
    content_html: '',
    placeholder: '開(kāi)始輸入...',
    isReadOnly: false,
    nodes: [{
      name: 'div',
      attrs: {
        class: 'div_class',
        style: 'line-height: 60px; color: red;'
      },
      children: [{
        type: 'text',
        text: 'RichText組件'
      }]
    }]
  },
  onLoad() {
    that = this;
  },
  onEditorReady() {
    // 輸入~編輯框
    wx.createSelectorQuery().select('#editor').context(function(res) {
      that.editorCtx = res.context;
      console.log("初始化成功:" + wx.getStorageSync("content"))
      if (wx.getStorageSync("content")) { // 設(shè)置~歷史值
        that.editorCtx.insertText(wx.getStorageSync("content")) // 注意:插入的是對(duì)象
      }
    }).exec()

  },
  // 獲取內(nèi)容
  onContentChange(e) {
    that.setData({
      content: e.detail,
    })
    wx.setStorageSync("content", e.detail)
  },
  // 顯示結(jié)果
  clickShowText(e) {
    that.setData({
      nodes: that.data.content.html,
      content_html: that.data.content.html
    })
  },
})
<view class="container">
  <view class="page-body">
    <editor id="editor" class="ql-container" placeholder="請(qǐng)輸入..." bindready="onEditorReady" bindinput="onContentChange">
    </editor>

    <view>
      <button bindtap="clickShowText">顯示結(jié)果</button>
    </view>

    <view class="show-rich">
      <rich-text nodes="{{nodes}}"></rich-text>
    </view>
  </view>
</view>
@import "../common/lib/weui.wxss";
@import "./assets/iconfont.wxss";

.ql-container {
  box-sizing: border-box;
  padding: 12px 15px;
  width: 100%;
  min-height: 30vh;
  height: 20px;
  background: #fff;
  margin-top: 20px;
  font-size: 14px;
  line-height: 1.5;
}

.ql-container2 {
  box-sizing: border-box;
  padding: 12px 15px;
  width: 100%;
  /* min-height: 30vh; */
  height: auto;
  border-top: 1px solid #ccc;
  border-bottom: 1px solid #ccc;
  background: #fff;
  margin-top: 20px;
  font-size: 14px;
  line-height: 1.5;
}

.ql-active {
  color: #06c;
}

.show-rich {
  padding: 12px 15px;
  width: 100%;
  margin-top: 20px;
  font-size: 14px;
  line-height: 1.5;
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容