本文實例講述了微信小程序rich-text富文本用法。分享給大家供大家參考,具體如下:

成都創新互聯公司是一家專業提供東明企業網站建設,專注與網站設計、成都網站設計、H5頁面制作、小程序制作等業務。10年已為東明眾多企業、政府機構等服務。創新互聯專業網站建設公司優惠進行中。
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: "我是文本節點,意外不?"
},
})
注意:
希望本文所述對大家微信小程序開發有所幫助。
文章標題:微信小程序rich-text富文本用法實例分析
網址分享:http://www.yijiale78.com/article34/pchcse.html
成都網站建設公司_創新互聯,為您提供品牌網站設計、標簽優化、網站制作、建站公司、小程序開發、商城網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯