GithubHelp home page GithubHelp logo

chenxingcai07 / wxparser Goto Github PK

View Code? Open in Web Editor NEW
15.0 2.0 2.0 23 KB

微信小程序 HTML 富文本解析

License: MIT License

JavaScript 100.00%
wxml html javascript wechat wechat-mini-program htmlparser

wxparser's Introduction

步骤


  1. wxParser 目录放到小程序项目的根目录下
  2. 在需要富文本解析的的 WXML 内引入 wxParser/index.wxml
  3. 在页面 JS 文件内使用 wxParser.parse(options) 方法解析 HTML 内容
  4. 在小程序项目根目录的 app.wxss 内引入 wxParser 的默认样式库

wxParser.parse(options) 方法的 options 参数说明

参数名 类型 必填 描述
bind String 要绑定的数据名称
html String HTML 内容
target Object 绑定数据的模块对象
enablePreviewImage Boolean 是否启用富文本内的图片预览功能,默认是
tapLink Function 点击超链接时的回调函数

示例


WXML

<import src="../../wxParser/index.wxml"/>
<view class="wxParser">
  <template is="wxParser" data="{{wxParserData:richText.nodes}}"/>
</view>

JS

const wxParser = require('../../wxParser/index');

Page({
  data: {},
  onLoad: function () {
    let that = this;
    let html = `<div style="color: #f00;">hello, wxParser!</div>`;

    wxParser.parse({
      bind: 'richText',
      html: html,
      target: that,
      enablePreviewImage: false, // 禁用图片预览功能
      tapLink: (url) => { // 点击超链接时的回调函数
        // url 就是 HTML 富文本中 a 标签的 href 属性值
        // 这里可以自定义点击事件逻辑,比如页面跳转
        wx.navigateTo({
          url
        });
      }
    });

  }
})

WXSS

@import '../wxParser/index.wxss'

注意


  • JS 中的 richText 可以自定义,但是必须要与 WXML 中的 richText 对应
  • 推荐把 template 放到 <view class="wxParser"></view> 内部,这样可以受 wxParser 控制并具有 wxParser 的一些默认样式
  • 不建议直接修改 wxParser 的默认样式(因为内容库样式会有定期更新),应该新增一个样式补丁文件用来自定义样式

wxparser's People

Contributors

chenxingcai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

wxparser's Issues

swiper中页面多了会很卡

我现在遇到一个优化问题。我的页面原始数据是html,现在用的wxParse转wxml显示,但是我的页面是swiper,swiper下面有很多页面,每个页面都要使用wxParse转wxml显示,加载效率有点低,所以会很卡。不知道有没有更好的解决办法

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.