1.富文本去除html标签
let richText = ' <p style="font-size: 25px;color: white"> sdaflsjf的丰富及饿哦塞尔</p><span>dsfjlie</span>';
let content = richText.replace(/<.+?>/g,'');
console.log(content);
content = content.replace(/ /ig,136);">/* 去除空格 */
content.replace(/\s/ig,179);">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);
if (richText.charCodeAt(i) > 128) {
strLength = strLength + 2;
if (strLength >= len) {
return temporaryText.Box-sizing: border-Box; color: rgb(0,temporaryText.length - 1) + "...";
}
} else {
strLength = strLength + 1;
if (strLength >= len) {
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 < '0' + Day + '日' : Day + '日';
let hours = date.getHours();
let minutes = date.getMinutes();
let hour = hours < '0' + hours + ':' : hours + ':';
let minute = minutes < '0' + minutes : minutes;
return Y + M + D + ' ' + hour + minute;
}
}
<div class="time">{{data.etime | localData}}</div>
3.微信小程序中使用过滤器
;
var M = Month < '-' : Month + var D = Day + '' : Day + '';
var H = hours < ':'
var m = minutes < '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中使用
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="" />
正在努力学习中,若对你的学习有帮助,留下你的印记呗(点个赞咯^_^)