国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 課堂 > 小程序 > 正文

如何去除富文本中的html標(biāo)簽及vue、react、微信小程序中的過濾器

2020-03-21 16:06:39
字體:
供稿:網(wǎng)友

在獲取富文本后,又只要顯示部分內(nèi)容,需要去除富文本標(biāo)簽,然后再截取其中一部分內(nèi)容;然后就是過濾器,在微信小程序中使用還是挺多次的,在vue及react中也遇到過

1.富文本去除html標(biāo)簽

去除html標(biāo)簽及 空格

let richText = ' <p style="font-size: 25px;color: white">       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來截取字符串 */if (content.length > 10) {  content = content.substring(0, 10) + '...';}console.log(content);/* 限制字?jǐn)?shù)后添加省略號(hào) */function formatRichText(richText) {  let temporaryText = '';  /* 設(shè)置多長后添加省略號(hào) */  const len = 142;  if (richText.length * 2 <= len) {    return richText;  }  /* 用于記錄文字內(nèi)容的總長度 */  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中使用過濾器

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ù),后面的是過濾器 */<div class="time">{{data.etime | localData}}</div>

3.微信小程序中使用過濾器

新建.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)簽來引入,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="" />

總結(jié)

以上所述是小編給大家介紹的如何去除富文本中的html標(biāo)簽及vue、react、微信小程序中的過濾器,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VEVB武林網(wǎng)網(wǎng)站的支持!


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 临沧市| 图木舒克市| 靖西县| 嘉鱼县| 长子县| 江达县| 阳春市| 区。| 洛隆县| 灵宝市| 怀宁县| 陵川县| 磴口县| 太谷县| 信宜市| 米脂县| 德江县| 讷河市| 志丹县| 桐梓县| 黑河市| 基隆市| 阿城市| 玉树县| 镇沅| 延庆县| 濮阳市| 水城县| 固安县| 大名县| 钟祥市| 敖汉旗| 抚顺市| 韶山市| 乐至县| 湄潭县| 合山市| 长沙市| 栾城县| 中方县| 武强县|