如何去除富文本中的html標籤及vue、react、微信小程序中的過濾器

這篇文章主要介紹瞭如何去除富文本中的html標籤及vue、react、微信小程序中的過濾器,在vue及react中經常會遇到,今天通過實例代碼給大家講解,需要的朋友可以參考下

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

1.富文本去除html標籤

去除html標籤及 空格

let richText = ' <p style="font-size: 25px;color: white">       sdaflsjf的豐富及餓哦塞爾</p><span>dsfjlie</span>';
/* 去除富文本中的html標籤 */
/* *、+限定符都是貪婪的,因爲它們會儘可能多的匹配文字,只有在它們的後面加上一個?就可以實現非貪婪或最小匹配。*/
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);
/* 限制字數後添加省略號 */
function formatRichText(richText) {
  let temporaryText = '';
  /* 設置多長後添加省略號 */
  const len = 142;
  if (richText.length * 2 <= len) {
    return richText;
  }
  /* 用於記錄文字內容的總長度 */
  let strLength = 0;
  for (let i = 0; i < richText.length; i++) {
    temporaryText = temporaryText + richText.charAt(i);
    /* charCodeAt()返回指定位置的字符的Unicode編碼,值爲128以下時一個字符佔一位,當值在128以上是一個字符佔兩位 */
    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中添加就可以了,| 前面的是參數,後面的是過濾器 */
<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(); //計算剩餘的小時
  var minutes = date.getMinutes(); //計算剩餘的分鐘
  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 />標籤來引入,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中使用,其實就是定義一個方法

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="" />

總結

以上所述是小編給大家介紹的如何去除富文本中的html標籤及vue、react、微信小程序中的過濾器,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對神馬文庫網站的支持!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章