1. 程式人生 > >微信小程式-利用wxParse將html轉為wxml

微信小程式-利用wxParse將html轉為wxml

2.1 在使用的View中引入WxParse模組

var WxParse = require('../../wxParse/wxParse.js');
  • 1

2.2 在使用的Wxss中引入WxParse.css,可以在app.wxss

@import "/wxParse/wxParse.wxss";
  • 1

3、資料繫結

var article = '<div>我是HTML程式碼</div>';
/**
* WxParse.wxParse(bindName , type, data, target,imagePadding)
* 1.bindName繫結的資料名(必填)
* 2.type可以為html或者md(必填)
* 3.data為傳入的具體資料(必填)
* 4.target為Page物件,一般為this(必填)
* 5.imagePadding為當圖片自適應是左右的單一padding(預設為0,可選)
*/
var that = this; WxParse.wxParse('article', 'html', article, that,5);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

4、模版引用

//這裡data中article為bindName
<template is="wxParse" data="{{wxParseData:article.nodes}}"/>
  • 1
  • 2

目錄結構如下:

這裡寫圖片描述

demo.wxml

<import src="../../wxParse/wxParse.wxml"/> 
<view class="wxParse">
    <template is="wxParse"
data="
{{wxParseData:article.nodes}}"/> </view> <view style="padding: 20px 10px; background-color:#eee;"> <block wx:for="{{replyTemArray}}" wx:key=""> 回覆{{index}}:<template is="wxParse" data="{{wxParseData:item}}"/> </block> </view>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

demo.js

var WxParse = require('../../wxParse/wxParse.js');
Page({
  data: {
  },
  onLoad: function () {
    var that = this;
    /**
     * 初始化emoji設定
     */
    WxParse.emojisInit('[]', "/wxParse/emojis/", {
      "00": "00.gif",
      "01": "01.gif",
      "02": "02.gif",
      "03": "03.gif",
      "04": "04.gif",
      "05": "05.gif",
      "06": "06.gif",
      "07": "07.gif",
      "08": "08.gif",
      "09": "09.gif",
      "09": "09.gif",
      "10": "10.gif",
      "11": "11.gif",
      "12": "12.gif",
      "13": "13.gif",
      "14": "14.gif",
      "15": "15.gif",
      "16": "16.gif",
      "17": "17.gif",
      "18": "18.gif",
      "19": "19.gif",
    });
    /**
     * html解析示例
     */
    var article = `
      <div
style="text-align:center;margin-top:10px;">
<img src="https://weappdev.com/uploads/default/original/1X/84512e0f4591bcf0dee42c3293f826e0c24b560c.jpg" alt="wxParse-微信小程式富文字解析元件Logo"> <h1 style="color:red;">wxParse-微信小程式富文字解析元件</h1> <h2>支援Html及markdown轉wxml視覺化</h2> </div> <div style="margin-top:10px;"> <h3 style="color: #000;">支援的標籤</h3> <blockquote>wxParse支援70%的html的標籤</blockquote> <div style="margin-top:10px;"> <span>span標籤</span> <strong>strong標籤</strong> </div> </div> <div style="margin-top:10px;"> <h3 style="color: #000;">支援內聯樣式style</h3> <blockquote>wxParse可以渲染原html帶有的style樣式</blockquote> <div style="margin-top:10px;"> <span>span標籤</span> <strong>strong標籤</strong> </div> </div> <div style="margin-top:10px;"> <h3 style="color: #000;">支援class潛入</h3> <blockquote>wxParse可以注入html帶有的class屬性</blockquote> </div> <div style="margin-top:10px;"> <h3 style="color: #000;">支援emojis小表情</h3> <blockquote>wxParse可以解析固定格式的小表情標籤</blockquote> <div style="margin-top:10px;"> <p>這裡可以解析出emoji的表情[00][01][02][03][04][05][06][07][08][09]</p> </div> </div> <div style="margin-top:10px;"> <h3 style="color: #000;">支援圖片自適應</h3> <blockquote>wxParse可以動態計算圖片大小並進行自適應適配</blockquote> <div style="margin-top:10px;"> <img src="http://a.hiphotos.baidu.com/image/pic/item/9a504fc2d5628535959cf4cf94ef76c6a6ef63db.jpg" alt=""> <img src="http://e.hiphotos.baidu.com/image/pic/item/48540923dd54564e1e1ac2d7b7de9c82d0584fe4.jpg" alt=""> </div> </div> <div style="margin-top:10px;"> <h3 style="color: #000;">支援圖片點選預覽,左右滑動預覽</h3> <blockquote>wxParse可以講一篇文章中的幾個圖片一起預覽</blockquote> <div style="margin-top:10px;"> 你可以點選上面的圖片,將會進入預覽檢視,同時左右滑動可以切換圖片預覽 </div> </div> <div style="margin-top:10px;"> <h3 style="color: #000;">支援多資料迴圈渲染</h3> <blockquote>wxParse支援特定設定下的多資料渲染,適用於在評論、多文字分別渲染等</blockquote> <div style="margin-top:10px; text-align:center;"> 請繼續向下看,迴圈渲染多條html評論 </div> </div> <div style="margin-top:10px;"> <h3 style="color: #000;">支援短table標籤</h3> <blockquote>wxParse目前對於table的支援比較有限</blockquote> <div style="margin-top:10px; text-align:center;"> <table> <tr> <th>標題1</th> <th>標題2</th> <th>標題3</th> <th>標題4</th> <th>標題5</th> </tr> <tr> <td>cell1</td> <td>cell2</td> <td>cell3</td> <td>cell4</td> <td>cell5</td> </tr> <tr> <td>cell1[03]</td> <td>cell2</td> <td>cell3</td> <td>cell4</td> <td>cell5</td> </tr> </table> </div> </div> `; WxParse.wxParse('article', 'html', article, that,5); /** * 多資料解析示例 */ var replyHtml0 = `<div style="margin-top:10px;height:50px;"> <p class="reply"> wxParse回覆0:不錯,喜歡[03][04] </p> </div>`; var replyHtml1 = `<div style="margin-top:10px;height:50px;"> <p class="reply"> wxParse回覆1:不錯,喜歡[03][04] </p> </div>`; var replyHtml2 = `<div style="margin-top:10px;height:50px;"> <p class="reply"> wxParse回覆2:不錯,喜歡[05][07] </p> </div>`; var replyHtml3 = `<div style="margin-top:10px;height:50px;"> <p class="reply"> wxParse回覆3:不錯,喜歡[06][08] </p> </div>`; var replyHtml4 = `<div style="margin-top:10px; height:50px;"> <p class="reply"> wxParse回覆4:不錯,喜歡[09][08] </p> </div>`; var replyHtml5 = `<div style="margin-top:10px;height:50px;"> <p class="reply"> wxParse回覆5:不錯,喜歡[07][08] </p> </div>`; var replyArr = []; replyArr.push(replyHtml0); replyArr.push(replyHtml1); replyArr.push(replyHtml2); replyArr.push(replyHtml3); replyArr.push(replyHtml4); replyArr.push(replyHtml5); for (let i = 0; i < replyArr.length; i++) { WxParse.wxParse('reply' + i, 'html', replyArr[i], that); if (i === replyArr.length - 1) { WxParse.wxParseTemArray("replyTemArray",'reply', replyArr.length, that) } } } })
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184

app.wxss

@import "/wxParse/wxParse.wxss";
  • 1

效果圖: 
這裡寫圖片描述

相關推薦

程式-利用wxParsehtml轉為wxml

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

程式使用wxParse解析html

1.需要下載wxParse檔案(點選即可下載) 2.新增到自己的工程內      3.引入wxss js wxml      1.wxss引入         @import"

程式通過WxParse解析HTML

目錄檔案  在需要用到的WXML頁面中先引入 <import src="/wxParse/wxParse.wxml"/> <import src="/wxParse/wxParse.wxml"/> 在需要用到的JS頁面中先引入 var W

程式使用wxParse解析html的實現示例

最近專案上遇到在微信小程式裡需要顯示新聞內容,新聞內容是通過介面讀取的伺服器中的富文字內容,是html格式的,小程式預設是不支援html格式的內容顯示的,那我們需要顯示html內容的時候,就可以通過wxParse來實現。 首先我們在github上下載wxParse

WxParse 程式富文字(html)編輯

GitHub下載地址:https://github.com/icindy/wxParse/tree/master/wxParse 參考:https://www.cnblogs.com/wesky/p/8066233.html 1)下載資料夾放到專案 utils資料夾下   2)若是新聞詳

程序使用wxParse解析html

樣式 新聞 hub req efi 上下 文件中 div github上 轉:http://www.jianshu.com/p/3de027555e77 最近項目上遇到在微信小程序裏需要顯示新聞內容,新聞內容是通過接口讀取的服務器中的富文本內容,是html格式的,小程序默認

程式--Web-View重新整理Html頁面(改)

最近進行小程式開發,因為主體上是複用了公眾號的內容,所有采用了Web-View載入H5的方式進行,其中有一個需求是連線藍芽裝置收集資料並上傳到後臺,點選H5中的按鈕,進入到藍芽採集頁面,然後在採集完資料後,跳回H5頁面並重新整理。 在H5中呼叫wx.miniProgram.navigateT

程式自動化測試-----生成html測試報告

1: 先下載HTMLTestRunner.py檔案:地址http://tungwaiyip.info/software/HTMLTestRunner.html 2:將該檔案儲存在python安裝路徑下的lib資料夾中。在檔案中能import HTMLTestRunner成功,即配置成功。

程式使用wxParse,解決圖片顯示路徑問題

我們經常用到釋出文章,用的是UEditor百度富文字編輯器,方便排版,儲存的也是html程式碼,這樣小程式解析出來的也是排版的樣式,但是使用wxParse解析html的時候,因為儲存的是圖片的相對路徑,所有在小程式解析的時候圖片不顯示,網上搜了好多,說當時圖片的時候在解析出來的圖片相對路徑前面加上域名,這樣也

程式利用animation實現元素翻轉到背面

效果如圖,正反面分別寫兩個元素,佈局讓兩個元素重疊 wxml的程式碼: <view class='rotateCtn'> <!--正面的框 --> <view class='{{frameClass1}}' data-id='1' bin

程式利用git提交專案

微信小程式提交專案:1、碼雲上建立一個專案 xiaochengxu2、本地建立一個資料夾,然後在控制檯使用 cd 資料夾名字 (將檔案拉到控制檯)3、使用 git init 命令 ,初始化一

程式 利用transform:Matrix(a,b,c,d,e,f) 實現圖片拖拽

微信小程式的樣式屬性 transform:Matrix()是一個非常強大的功能,通過這個組建可以實現非常多的圖形變化,位移等功能。 下面上位移樣例: 如圖,我要在紅色區域內移動圖片,而這個圖片是有一

程式--Web-View重新整理Html頁面

最近進行小程式開發,因為主體上是複用了公眾號的內容,所有采用了Web-View載入H5的方式進行,其中有一個需求是連線藍芽裝置收集資料並上傳到後臺,點選H5中的按鈕,進入到藍芽採集頁面,然後在採集完資料後,跳回H5頁面並重新整理。 在H5中呼叫wx.miniProgram.n

程式載入和解析html標籤

    小程式的頁面可以是使用wx的語法編寫的頁面,也可以是第三方html頁面,還可以是wx的頁面然後使用wxParse解析html的標籤最終構成小程式可以載入的wx頁面。今天用到了第三種。    首先需要下載wxParse外掛,我生成了雲盤連結。    下載之後解壓並放置在

程式開發 檔案結構 js、wxml、wxss、json

檔案結構小程式包含一個描述整體程式的 app 和多個描述各自頁面的 page。一個小程式主體部分由三個檔案組成,必須放在專案的根目錄,如下:一個小程式頁面由四個檔案組成,分別是:檔案型別必填作用js是頁

程式顯示 html wepy + wxParse

本問出自: http://blog.csdn.net/wyk304443164 修改自: http://www.wxappclub.com/topic/961 本著不重複造輪子的基礎上我找到了上面這篇。發現它不能正常執行,也就是顯示的是空白的。

程式開發——使用wxParse外掛實現html程式碼的支援

前言: 大家都知道,無論是微信小程式還是支付寶小程式都不支援html程式碼的展示的,甚至說你想貼個純html程式碼做demo都不方便,更不用說解析html了。那麼怎麼解決這個問題呢? 解決方案: 使用WxParse外掛(微信小程式富文字解析元件)就能解決這個問題。本來這個外掛是專為微信小程式設計的,但是

程式:使用wxParse解析HTML

      1、下載 wxParse ,放到utils目錄下       2、在JS頁面引入: import WxParse from '../../utils/wxParse/wxParse' Page({ data:{ contentHTML:'' //

(轉載)程式 —— 程式解析html富文字外掛wxParse

下載並把wxParse放到小程式的目錄中把wxParse放在與page同一級目錄 wxParse裡的emojis包是可要可不要的。 12引用wxParse - wxml中 12<import src="../../wxParse/wxParse.wxml" />

程式——wxParse使用方法

wxParse是一個微信小程式富文字解析元件。現在小程式裡面自帶了一個<rich-text>元件也能解析富文字,但是表現不盡人意。所以我還是採用的wxParse來解析富文字的。 wxParse git地址:https://github.com/icindy/wxParse。可以先稍作了