经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » 微信小程序 » 查看文章
微信小程序rich-text富文本用法实例分析
来源:jb51  时间:2019/5/20 10:49:05  对本文有异议

本文实例讲述了微信小程序rich-text富文本用法。分享给大家供大家参考,具体如下:

rich-text是一个新增的微信小程序插件,从基础库1.4.0开始,低版本需要做兼容处理

nodes属性可为Array和String类型,但推荐使用Array.由于String类型最终也会转为Array类型

nodes分为支持两种节点,分别为元素节点(type=node ,默认为元素节点)文本节点(type=text)

元素节点

name 标签名 String 支持部分受信任的HTML节点
attrs 属性 Object 支持部分受信任的属性,遵循Pascal命名法
children 子节点列表 Array 结构和nodes一致

  1. <!-- rich-text.wxml -->
  2. <rich-text nodes="{{nodes}}" bindtap="tap"></rich-text>
  3. <!--{{nodes}}其中的变量名与data中名字相同-->
  4. <!--支持默认事件tap、touchstart、touchmove、touchcancel、touchend和longtap-->
  5.  
  1. // rich-text.js
  2. Page({
  3. data: {
  4. nodes: [{
  5. name: 'div',
  6. attrs: {
  7. class: 'div_class',
  8. style: 'width : 100px; height : 100px; color: red;'
  9. },
  10. children: [{
  11. type: 'text',
  12. text: 'Hello World!'
  13. }]
  14. }]
  15. },
  16. tap() {
  17. console.log('tap')
  18. }
  19. })
  20.  

如果页面中存在多个富文本,富文本中存在多个孩子,请看下例:

  1. <!-- rich-text.wxml -->
  2. <rich-text nodes="{{nodes}}"></rich-text>
  3. <rich-text nodes="{{nodes1}}"></rich-text>
  4.  
  1. // rich-text.js
  2. Page({
  3. data: {
  4. nodes: [{
  5. name: 'div',
  6. attrs: {
  7. class: 'div_class',
  8. style: 'width : 100px; height : 100px; color: red;'
  9. },
  10. children: [{
  11. type: 'text',
  12. text: 'Hello World!'
  13. }]
  14. }],
  15. nodes1: [{
  16. name: 'p',
  17. attrs: {
  18. class: 'p_class',
  19. style: 'text-align : center; color: green;'
  20. },
  21. children: [{
  22. type: 'text',
  23. text: '我是p标签!!!'
  24. },{
  25. name: "span",
  26. attrs: {
  27. style: "color:red",
  28. class: "span_class"
  29. },
  30. children: [{
  31. type: "text",
  32. text: '我是span标签,哈哈哈哈'
  33. }]
  34. }]
  35. }]
  36. },
  37. })
  38.  

文本节点

text 文本 String 支持entities

  1. <!-- rich-text.wxml -->
  2. <rich-text nodes="{{nodes}}"></rich-text>
  3.  
  1. // rich-text.js
  2. Page({
  3. data: {
  4. nodes: "我是文本节点,意外不?"
  5. },
  6. })
  7.  

注意:

  • 全局支持class和style属性,不支持id属性。
  • nodes 不推荐使用 String 类型,性能会有所下降
  • rich-text 组件内屏蔽所有节点的事件。
  • name 属性大小写不敏感
  • 如果使用了不受信任的HTML节点,该节点及其所有子节点将会被移除,受信任的html节点请看官方文档
  • img 标签仅支持网络图片

希望本文所述对大家微信小程序开发有所帮助。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号