您好,登錄后才能下訂單哦!
小編給大家分享一下怎樣去除富文本中的html標(biāo)簽及vue、react、微信小程序中的過(guò)濾器,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
在獲取富文本后,又只要顯示部分內(nèi)容,需要去除富文本標(biāo)簽,然后再截取其中一部分內(nèi)容;然后就是過(guò)濾器,在微信小程序中使用還是挺多次的,在vue及react中也遇到過(guò)
1.富文本去除html標(biāo)簽
去除html標(biāo)簽及 空格
let richText = ' <p > sdaflsjf的豐富及餓哦塞爾</p><span>dsfjlie</span>'; /* 去除富文本中的html標(biāo)簽 */ /* *、+限定符都是貪婪的,因?yàn)樗鼈儠?huì)盡可能多的匹配文字,只有在它們的后面加上一個(gè)?就可以實(shí)現(xiàn)非貪婪或最小匹配。*/ let content = richText.replace(/<.+?>/g, ''); console.log(content); /* 去除 */ content = content.replace(/ /ig, ''); console.log(content); /* 去除空格 */ content = content.replace(/\s/ig, ''); console.log(content);
截取字符串
content = formatRichText(content); console.log(content); /* 使用substring來(lái)截取字符串 */ if (content.length > 10) { content = content.substring(0, 10) + '...'; } console.log(content); /* 限制字?jǐn)?shù)后添加省略號(hào) */ function formatRichText(richText) { let temporaryText = ''; /* 設(shè)置多長(zhǎng)后添加省略號(hào) */ const len = 142; if (richText.length * 2 <= len) { return richText; } /* 用于記錄文字內(nèi)容的總長(zhǎng)度 */ let strLength = 0; for (let i = 0; i < richText.length; i++) { temporaryText = temporaryText + richText.charAt(i); /* charCodeAt()返回指定位置的字符的Unicode編碼,值為128以下時(shí)一個(gè)字符占一位,當(dāng)值在128以上是一個(gè)字符占兩位 */ if (richText.charCodeAt(i) > 128) { strLength = strLength + 2; if (strLength >= len) { return temporaryText.substring(0, temporaryText.length - 1) + "..."; } } else { strLength = strLength + 1; if (strLength >= len) { return temporaryText.substring(0, temporaryText.length - 2) + "..."; } } } return temporaryText; }
2.vue中使用過(guò)濾器
filters: { localData(value) { let date = new Date(value * 1000); let Month = date.getMonth() + 1; let Day = date.getDate(); let Y = date.getFullYear() + '年'; let M = Month < 10 ? '0' + Month + '月' : Month + '月'; let D = Day + 1 < 10 ? '0' + Day + '日' : Day + '日'; let hours = date.getHours(); let minutes = date.getMinutes(); let hour = hours < 10 ? '0' + hours + ':' : hours + ':'; let minute = minutes < 10 ? '0' + minutes : minutes; return Y + M + D + ' ' + hour + minute; } } /* 使用,直接在div中添加就可以了,| 前面的是參數(shù),后面的是過(guò)濾器 */ <div class="time">{{data.etime | localData}}</div>
3.微信小程序中使用過(guò)濾器
新建.wxs文件
var localData = function (value) { var date = getDate(value * 1000); var Month = date.getMonth() + 1; var Day = date.getDate(); var hours = date.getHours(); //計(jì)算剩余的小時(shí) var minutes = date.getMinutes(); //計(jì)算剩余的分鐘 var Y = date.getFullYear() + '-'; var M = Month < 10 ? '0' + Month + '-' : Month + '-'; var D = Day + 1 < 10 ? '0' + Day + '' : Day + ''; var H = hours < 10 ? '0' + hours + ':' : hours + ':' var m = minutes < 10 ? '0' + minutes : minutes; return Y+M + D + " " + H + m; } module.exports = { localData: localData }
使用,用<wxs />標(biāo)簽來(lái)引入,src為路徑,module為引入的文件模塊名
<wxs src="./filters.wxs" module="tool" /> <text class="scoreText">{{tool.filterScore(item.shop.score)}}分</text>
直接在.wxml文件中用<wxs></wxs>包裹
<wxs module="foo"> var some_msg = "hello world"; module.exports = { msg : some_msg, } </wxs> <view> {{foo.msg}} </view>
4.react中使用
react中使用,其實(shí)就是定義一個(gè)方法
import noBanner from '@/assets/storeDetail/no-banner.jpg' const filterImg = item => { let bgImg; if (item.shopimages == null) { bgImg = noBanner; } else { bgImg = item.shopimages[0]; } return bgImg; }; /* 使用 */ <img src={filterImg(storeitem)} className={style.topImg} alt="" />
看完了這篇文章,相信你對(duì)“怎樣去除富文本中的html標(biāo)簽及vue、react、微信小程序中的過(guò)濾器”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。