本文实例讲述了微信小程序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一致 |
<!-- rich-text.wxml -->
<rich-text nodes="{{nodes}}" bindtap="tap"></rich-text>
<!--{{nodes}}其中的变量名与data中名字相同-->
<!--支持默认事件tap、touchstart、touchmove、touchcancel、touchend和longtap-->
// rich-text.js
Page({
data: {
nodes: [{
name: 'div',
attrs: {
class: 'div_class',
style: 'width : 100px; height : 100px; color: red;'
},
children: [{
type: 'text',
text: 'Hello World!'
}]
}]
},
tap() {
console.log('tap')
}
})
如果页面中存在多个富文本,富文本中存在多个孩子,请看下例:
<!-- rich-text.wxml -->
<rich-text nodes="{{nodes}}"></rich-text>
<rich-text nodes="{{nodes1}}"></rich-text>
// rich-text.js
Page({
data: {
nodes: [{
name: 'div',
attrs: {
class: 'div_class',
style: 'width : 100px; height : 100px; color: red;'
},
children: [{
type: 'text',
text: 'Hello World!'
}]
}],
nodes1: [{
name: 'p',
attrs: {
class: 'p_class',
style: 'text-align : center; color: green;'
},
children: [{
type: 'text',
text: '我是p标签!!!'
},{
name: "span",
attrs: {
style: "color:red",
class: "span_class"
},
children: [{
type: "text",
text: '我是span标签,哈哈哈哈'
}]
}]
}]
},
})
文本节点
text | 文本 | String | 是 | 支持entities |
<!-- rich-text.wxml -->
<rich-text nodes="{{nodes}}"></rich-text>
// rich-text.js
Page({
data: {
nodes: "我是文本节点,意外不?"
},
})
注意:
希望本文所述对大家微信小程序开发有所帮助。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。