vue如何过滤html标签,去除富文本中的html标签及vue、react、微信小顺序中的过滤器...
在獵取富文本后,又只需顯現部分內容,須要去除富文本標簽,然后再截取個中一部分內容;然后就是過濾器,在微信小順序中運用照樣挺屢次的,在vue及react中也遇到過
1.富文本去除html標簽去除html標簽及?空格let richText = '
? ? ? ?sdaflsjf的雄厚及餓哦塞爾
dsfjlie';/* 去除富文本中的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中增加就能夠了,| 前面的是參數,背面的是過濾器 */
{{data.etime | localData}}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
}運用,用標簽來引入,src為途徑,module為引入的文件模塊名
{{tool.filterScore(item.shop.score)}}分直接在.wxml文件頂用包裹
var some_msg = "hello world";
module.exports = {
msg : some_msg,
}
{{foo.msg}}
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;
};
/* 運用 */
正在努力進修中,若對你的進修有協助,留下你的印記唄(點個贊咯^_^)
總結
以上是生活随笔為你收集整理的vue如何过滤html标签,去除富文本中的html标签及vue、react、微信小顺序中的过滤器...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 本科计算机应用水平,湖南工程学院全日制非
- 下一篇: 计算机软件水平考试调整,全国计算机等级考