微信小程序輸出html內容數據插件wxParse,能夠把帶html標籤的數據輸出爲微信小程序正常顯示的格式,測試可用,不過外觀樣式,可能須要根據你的需求再調整一下,wxParse插件帶有演示,也有使用文檔說明。
下載地址:https://github.com/icindy/wxParsecss
- wxParse/ -wxParse.js(必須存在) -html2json.js(必須存在) -htmlparser.js(必須存在) -showdown.js(必須存在) -wxDiscode.js(必須存在) -wxParse.wxml(必須存在) -wxParse.wxss(必須存在) -emojis(可選)
//在使用的View中引入WxParse模塊 var WxParse = require('../../wxParse/wxParse.js');
//在使用的Wxss中引入WxParse.css,能夠在app.wxss @import "/wxParse/wxParse.wxss";
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);
//這裏data中article爲bindName <template is="wxParse" data="{{wxParseData:article.nodes}}"/>
/** * WxParse.emojisInit(reg,baseSrc,emojis) * 1.reg,如格式爲[00]=>賦值 reg='[]' * 2.baseSrc,爲存儲emojis的圖片文件夾 * 3.emojis,定義表情鍵值對 */ 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", });
介紹如何使用wxParse在回覆等多條HTML共同渲染的方法html
/** * WxParse.wxParseTemArray(temArrayName,bindNameReg,total,that) * 1.temArrayName: 爲你調用時的數組名稱 * 3.bindNameReg爲循環的共同體 如綁定爲reply1,reply2...則bindNameReg = 'reply' * 3.total爲reply的個數 * 懶人建站http://www.51xuediannao.com/ * var that = this; WxParse.wxParseTemArray("replyTemArray",'reply', replyArr.length, that) */
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) } }
<block wx:for="{{replyTemArray}}" wx:key=""> 回覆{{index}}:<template is="wxParse" data="{{wxParseData:item}}"/> </block>
下載地址:https://github.com/icindy/wxParsenode