微信小程序rich-text富文本用法實(shí)例分析
本文實(shí)例講述了微信小程序rich-text富文本用法。分享給大家供大家參考,具體如下:
rich-text是一個(gè)新增的微信小程序插件,從基礎(chǔ)庫(kù)1.4.0開(kāi)始,低版本需要做兼容處理
nodes屬性可為Array和String類型,但推薦使用Array.由于String類型最終也會(huì)轉(zhuǎn)為Array類型
nodes分為支持兩種節(jié)點(diǎn),分別為元素節(jié)點(diǎn)(type=node ,默認(rèn)為元素節(jié)點(diǎn))和文本節(jié)點(diǎn)(type=text)
元素節(jié)點(diǎn)
name | 標(biāo)簽名 | String | 是 | 支持部分受信任的HTML節(jié)點(diǎn) |
attrs | 屬性 | Object | 否 | 支持部分受信任的屬性,遵循Pascal命名法 |
children | 子節(jié)點(diǎn)列表 | Array | 否 | 結(jié)構(gòu)和nodes一致 |
<!-- rich-text.wxml --> <rich-text nodes="{{nodes}}" bindtap="tap"></rich-text> <!--{{nodes}}其中的變量名與data中名字相同--> <!--支持默認(rèn)事件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') } })
如果頁(yè)面中存在多個(gè)富文本,富文本中存在多個(gè)孩子,請(qǐng)看下例:
<!-- 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標(biāo)簽!!!' },{ name: "span", attrs: { style: "color:red", class: "span_class" }, children: [{ type: "text", text: '我是span標(biāo)簽,哈哈哈哈' }] }] }] }, })
文本節(jié)點(diǎn)
text | 文本 | String | 是 | 支持entities |
<!-- rich-text.wxml --> <rich-text nodes="{{nodes}}"></rich-text>
// rich-text.js Page({ data: { nodes: "我是文本節(jié)點(diǎn),意外不?" }, })
注意:
- 全局支持class和style屬性,不支持id屬性。
- nodes 不推薦使用 String 類型,性能會(huì)有所下降
- rich-text 組件內(nèi)屏蔽所有節(jié)點(diǎn)的事件。
- name 屬性大小寫不敏感
- 如果使用了不受信任的HTML節(jié)點(diǎn),該節(jié)點(diǎn)及其所有子節(jié)點(diǎn)將會(huì)被移除,受信任的html節(jié)點(diǎn)請(qǐng)看官方文檔
- img 標(biāo)簽僅支持網(wǎng)絡(luò)圖片
希望本文所述對(duì)大家微信小程序開(kāi)發(fā)有所幫助。
相關(guān)文章
js實(shí)現(xiàn)網(wǎng)站最上邊可關(guān)閉的浮動(dòng)廣告條代碼
這篇文章主要介紹了js實(shí)現(xiàn)網(wǎng)站最上邊可關(guān)閉的浮動(dòng)廣告條代碼,涉及javascript基于鼠標(biāo)事件操作頁(yè)面元素樣式的實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-09-09js如何獲取file控件的完整路徑具體實(shí)現(xiàn)代碼
需要隱藏input file然后獲取它的值,但連jquery都無(wú)法獲取它的值,下面與大家分享下使用js的具體獲取方法,感興趣的朋友可以參考下哈2013-05-05javascript使用正則實(shí)現(xiàn)去掉字符串前面的所有0
這篇文章主要介紹了javascript使用正則實(shí)現(xiàn)去掉字符串前面的所有0,需要的朋友可以參考下2018-07-07