1. 場景
在小程序開發過程中,請求回來的數據,有時候會帶有h5標簽,直接展示的話: 會在頁面上顯示出h5標簽
方法:使用 <rich-text mode="{{info}}"> </rich-text> 解析h5標簽
現象:解析出的圖片、文字等樣式需要設置
2. 方法
(1) 使用正則給標簽加上類名
(2) 在此類名中寫上自己想要的樣式
(3) 如
- js中--使用正則加類名 其中:info是從請求回來的數據
info = info .replace(/<p([\s\w"=\/\.:;]+)((?:(style="[^"]+")))/ig, '<p') .replace(/<p([\s\w"=\/\.:;]+)((?:(class="[^"]+")))/ig, '<p') .replace(/<p>/ig, '<p class="p_class">') .replace(/<img([\s\w"-=\/\.:;]+)((?:(height="[^"]+")))/ig, '<img$1') .replace(/<img([\s\w"-=\/\.:;]+)((?:(width="[^"]+")))/ig, '<img$1') .replace(/<img([\s\w"-=\/\.:;]+)((?:(style="[^"]+")))/ig, '<img$1') .replace(/<img([\s\w"-=\/\.:;]+)((?:(alt="[^"]+")))/ig, '<img$1') .replace(/<img([\s\w"-=\/\.:;]+)/ig, '<img$1 class="pho"')
this.setData({
info: info
})
- less中--定義樣式表
rich-text { width: 670rpx; display: block; font-size: 28rpx; color: #353535; letter-spacing: 1rpx; .p_class { line-height: 46rpx; text-indent: 60rpx; margin-bottom: 20rpx; } .pho { display: block; width: 670rpx !important; height: 330rpx; border-radius: 20rpx; } }