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;

};

/* 使用 */

正在努力学习中,若对你的学习有帮助,留下你的印记呗(点个赞咯^_^)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值