一、小程序内嵌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>

原文地址:https://www.cnblogs.com/fger/p/11108552.html

时间: 2024-08-28 16:38:25

一、小程序内嵌Html示例的相关文章

小程序内嵌网页的开放是一场创业公司的及时雨吗

11.2晚小程序突然开放两个重磅能力:1.小程序内可直接打开网页,2.小程序可关联500个公众号.通过研究,我们发现这次新能力的开放,促使小程序推动微信的春秋战国时代来临.有开发者感叹,不懂小程序的人,以后就不要出来谈营销啦!接下来,我们就听听萤火新媒萤连长从三个方向为大家解读,为什么新能力的开放是一场创业公司的及时雨.一.对于开发者.运营者有哪些好处1.小程序有了自己的流量用户使用小程序时,可以从小程序直接进入到内嵌网页,同时也支持用户从内嵌网页返回小程序.这样一来,小程序也可以做成小的流量分

[小程序开发] 微信小程序内嵌网页web-view开发教程

为了便于开发者灵活配置小程序,微信小程序开放了内嵌网页能力.这意味着小程序的内容不再局限于pages和large,我们可以借助内嵌网页丰富小程序的内容.下面附上详细的开发教程(含视频操作以及注意事项) 一.小程序内嵌网页web-view教程 1) 微信公众平台,登录小程序账号 2) 左侧-设置-开发设置-业务域名-配置 3) 小程序管理员微信扫码 4) 填写小程序业务域名,域名需ICP备案 5) 下载校检文件上传至服务器指定目录,保存 6) 小程序业务域名配置完成7) 打开微信开发者工具,添加小

微信小程序内嵌网页的一些(最佳)实践

前言 3 个月前,微信小程序推出了 web-view 组件引发了一波小高潮,笔者所在的大前端团队写过一篇浅析,详情可见:浅谈微信小程序前端生态. 我们曾大胆猜想,这一功能,可能直接导致小程序数量增长迎来一波高峰. 毕竟磨刀霍霍却一直资源不足的团队应该不少,现在可以把已有 H5 应用嵌入到小程序 web-view 容器中,以最低的开发成本坐蹭微信流量红利,何乐而不为呢? 我们也曾畅想也许"小程序页面+ web 页"混合开发(甚至 web 更重)会成为以后的新趋势. 2M 代码限制(如今已

小程序内嵌H5——判断小程序环境的坑

现在各种小程序风靡,这边H5的需求还没有搞定,产品又要求做小程序版本,做可以,关键是618前上线,我-- whatever,618要做推广,日期订了,剩下的就只能是排期,定方案,尽可能完成. 最后和产品商量之后的决定是:小程序中特有的营销推广的页面,用小程序编写,剩下的黄金流程,内嵌H5解决. 听起来好像很简单,but,没有想象中那么简单. 判断是否在小程序中 这个问题,网上一搜,答案很多,有说用UA的,有说用微信注入的JS对象的,有说用原生提供判断方法的. UA的有一个很大的问题,iOS判断不

记小程序内嵌商城踩过的坑

最近接到需求,需要做一个简单小程序管理自家的设备,但又想增加销售渠道,故打算做个小程序并内嵌自家微信公众号商城. 总结主要问题: 1.小程序返回问题 2.支付问题 3.商城访问权限问题 4.ssl证书问题 5.端口号冲突问题 问题解决: 1.问题详情:小程序利用<web-view>嵌入商城界面,左上角返回发现会先出现一次空白页,第二次才会回到小程序页面    原因查找:进入商城时首先会有个微信授权页,然后才会进入微信商城,所以要返回两次    问题处理:由于商城里面还有多个界面,所以不能采用网

二、小程序内嵌Html基础格式说明

1.index.js文件更改 var WxParse = require('../../wxParse/wxParse.js'); Page({ data: { }, onLoad: function () { var that = this; var article = ` <!DOCTYPE html> <html> <head> <title></title> </head> <body> <h1>Hel

微信小程序内联h5页面,实现分享

在小程序内直联h5的页面(pages/webview/webview.js),该页面为<web-view>的容器,使用<web-view>组件 <web-view wx:if="{{h5url}}" src="{{h5url}}" bindmessage="h5PostMessage" ></web-view> 需要bindmessage事件和postMessage方法 bindmessage:网页

应用程序内嵌资源文件[训练数据]

Windows exe/dll内嵌数据 ? 调用资源的方式 exe/dll有对应的入口函数main/_tmain/winmain/_dllmain等:透过这些入口函数可以获取exe文件路径或者文件的handle[exe通过入口函数拿到路径后调用LoadLibrary得到句柄,dll直接从入口函数得到句柄]:静态库没有入口函数拿不到句柄不能进行资源嵌入; ? 调用代码示例: [纯数据文件,如果是media文件{音视频文件}或者数据库db文件等需要用专门库解析的文件则可以考虑将读取的数据指针的内容写

微信小程序内训笔记

2016年9月22日凌晨微信官方正式宣布“小程序”开始内测,有“微信之父”之称.腾讯集团高级执行副总裁张小龙在2016年末对外宣布“小程序“应用将于2017年1月9日正式推出 这一次微信还是按照惯例,通过机器跑出的数据,首先将“小程序”开放给了200名拥有微信服务号的开发者进行内测,而且目前开发者发布的小程序无法在用户的客户端显示 简单说,微信“小程序”可以为开发者提供基于微信的表单.导航.地图.媒体和位置等开发组件,让他们在微信的网页里构建一个HTML 5应用.同时微信还开放了登录和微信支付等