uniapp textarea编辑保留空格换行

rich-text | 微信开放文档

 实现:

textFormat(val) {
			// 格式化文字展示文本域格式文字
			if (val) {
				console.log(val)
				let newString = val.replace(/\n/g, '_@').replace(/\r/g, '_#');
				newString = newString.replace(/_#_@/g, '<br/>'); // IE7-8
				newString = newString.replace(/_@/g, '<br/>'); // IE9、FF、chrome
				newString = newString.replace(/\s/g, '&nbsp;'); // 空格处理
				return newString;
			}
		},

 示例:

<template>
	<view class="u-p-b-30">
		<view class="cu-card article" :class="isCard ? 'no-card' : ''">
			<view class="cu-item shadow" v-for="(item, index) in job_list" :key="index" @click="todetail(item.id)">
				<view class="title">
					<view class="">{{ item.name }}</view>
				</view>
				<view class="content">
					<view class="desc">
						<view class="text-content line-4-hidden" >
							<rich-text :nodes="item.desc" space='emsp'></rich-text>
						</view>
						<view class="flex justify-end mt-10 flex-center">
							<u-icon name="clock" size="38"></u-icon>
							<text class="bg-white">{{item.openedDate}}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<add-icon @toAddPage="toAddPage"></add-icon>
		<view class=" ptb-10"><u-loadmore :status="loadStatus" /></view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			modalName: null,
			title: 'Hello',
			job_list: [],
			loadStatus: 'loading',
			isCard: false,
			isNoMore: true
		};
	},
	onLoad() {},
	onShow() {
		getApp().reqLogin(() => {
			uni.hideLoading();
			this.getIndex();
		});
	},
	methods: {
		handelTextMore(index) {
			this.job_list[index].isNoMore = !this.job_list[index].isNoMore;
		},
		getIndex() {
			this.request.reqIndex().then(res => {
				this.loadStatus = 'nomore';
				console.log(res);
				if (res.status == 10001) {
					uni.redirectTo({
						url: '/pages/login/open'
					});
				}
				const list = res.data.job_list;

				list.forEach(item => {
					item.isNoMore = true;
					item.openedDate = item.openedDate.substr(0, 10);
					item.desc = this.textFormat(item.desc);
				});
				this.job_list = list;
			});
		},
		textFormat(val) {
			// 格式化文字展示文本域格式文字
			if (val) {
				console.log(val)
				let newString = val.replace(/\n/g, '_@').replace(/\r/g, '_#');
				newString = newString.replace(/_#_@/g, '<br/>'); // IE7-8
				newString = newString.replace(/_@/g, '<br/>'); // IE9、FF、chrome
				newString = newString.replace(/\s/g, '&nbsp;'); // 空格处理
				return newString;
			}
		},
		todetail(id){
			uni.navigateTo({
				url:'/pages/index/detail/detail?id='+id
			})
		},
		toSub() {
			console.log('触发');
			uni.navigateTo({
				url: '/pages/index/add/add'
			});
		},

	
		toAddPage() {
			uni.navigateTo({
				url: '/pages/index/add/add'
			});
		}
	}
};
</script>

<style lang="scss">
.see-more {
	justify-content: center;
	font-size: 28rpx;
	position: relative;
	.iconfont {
		font-size: 26rpx;
	}
}
.cu-card.article > .cu-item .title {
	line-height: 1.5;
	padding: 0 20rpx !important;
}
.text-content {
	margin-top: 20rpx;
	line-height: 1.3 !important;
	color: #666 !important;
}
.cu-card > .cu-item {
	margin: 20rpx !important;
	padding-top: 20rpx;
}
.cu-card.article > .cu-item .content .text-content {
	height: auto !important;
}
.cu-card.article > .cu-item {
	padding-bottom: 0rpx !important;
}
.cu-card.article > .cu-item .content {
	padding: 20rpx !important;
	padding-top: 0rpx !important;
}
</style>

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值