微信小程序中显示html富文本的方法

使用方法:
git地址:https://github.com/icindy/wxParse

一、下载wxParse文件
 
image.png
  • wxParse/
    -wxParse.js(必须存在)
    -html2json.js(必须存在)
    -htmlparser.js(必须存在)
    -showdown.js(必须存在)
    -wxDiscode.js(必须存在)
    -wxParse.wxml(必须存在)
    -wxParse.wxss(必须存在)
    -emojis(可选)
二、在要引入的页面的js文件中,引入文件

//在使用的View中引入WxParse模块
var WxParse = require('../../wxParse/wxParse.js');
//在使用的Wxss中引入WxParse.css,可以在app.wxss
@import "/wxParse/wxParse.wxss";

三、数据绑定

在page.js文件中绑定数据
如:

onLoad: function (options) {
  var that = this;
  var detail_content ="<div>我是HTML代码</div>";
  WxParse.wxParse('detail_content', 'html', detail_content, that, 5);
} //注意第一个参数需要与wxml中的一致

四、在page的wxml中引入模板

<view class='product-content'>
  <import src="../../wxParse/wxParse.wxml" />
  <view class="wxParse">
    <scroll-view style="height:2000rpx;" scroll-y='true'>
      <template is="wxParse" data="{{wxParseData:detail_content.nodes}}" />
    </scroll-view>
  </view>
</view>

转载地址https://www.jianshu.com/p/9a0f74b3c7c8

原文地址:https://www.cnblogs.com/520BigBear/p/13646326.html