一、小程序內嵌Html示例


 小程序內嵌Html

1、下載wxParse:https://github.com/icindy/wxParse

2、下載完成后將插件目錄下的wxParse文件夾拷貝到項目目錄下

(文件夾明細)

3、全局配置文件app.wxss中導入wxParse的樣式表

@import "/wxParse/wxParse.wxss";

(以上只是僅在app.wxss引入配置,其他頁面布局未動)

4、下面是在我們自己需要寫的頁面操作

在需要加載html代碼的頁面js文件中引用wxParse.js文件並通過WxParse.wxParse的方法來設置html內容

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 = `< !DOCTYPE HTML ><!--注釋: wxParse試驗文本-->
      <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;">支持video</h3>
        <div style="margin-top:10px;">
            <video src="http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400"></video>
        </div>
    </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 style="color: red;">strong標簽</strong>
        </div>
    </div>

    <div style="margin-top:10px;">
        <h3 style="color: #000;">支持的標簽ul/li</h3>
        <blockquote>帶有內聯的li</blockquote>
        <div style="margin-top:10px;">
            <ul>
                <li style="color: red;">我是li 紅色</li>
                <li style="color: blue;">我是li 藍色</li>
                <li style="color: yelloe;">我是li 黃色</li>
            </ul>
        </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</td>
            <td>cell2</td>
            <td>cell3</td>
            <td>cell4</td>
            <td>cell5</td>
        </tr>
    </table>
        </div>
    </div>
    <!--ap-->
    `;


    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)
      }
    }
  }


})

 

  

5、頁面中引用模

 

<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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM