手撕CSDN发布文章时的动态添加标签

前提

这个效果我是写在vue上的,小伙伴们需要会vue的知识,不然可能看不懂,其实实现的难度并不高

效果

在这里插入图片描述

代码

<template>
	<div>
		<el-row :gutter="15" >
		  <el-col :span="4">文章标签:</el-col>
		  <el-col :span="20">
			<span v-if="isLabel">
				<input v-model="TemLabel"  class="diaInput" type="text"/>
				&nbsp;<i @click="LabelFalse()" class="el-icon-close" style="cursor: pointer;"></i>				
			</span>   
			<span  v-for="item in temporaryLabel">
				<input  :value="item" class="diaInput" type="text"/>
				&nbsp;<i @click="LabelFalse(item)" class="el-icon-close" style="cursor: pointer;"></i>				
			</span>   
			<span @click="addLabel" style="color: #007FFF;cursor: pointer;">
				 <i  class="el-icon-circle-plus-outline"></i>&nbsp;添加标签
			</span>
		  </el-col>
		</el-row>
	</div>	
<template>		
<script>
	export default{
		name:'Dialog',
		data () {
		      return {
				isLabel:false, //标签显示判断
				TemLabel:'',//标签临时字符串
				temporaryLabel:[], //标签临时数组
		      }
		},
		methods:{
			addLabel(){
				this.isLabel=true
				if(this.TemLabel.length<=0){				
					return
				}
				if(this.TemLabel!=" "){
					this.temporaryLabel.unshift(this.TemLabel);
				}
				this.TemLabel=" ";			
			},
			LabelFalse(val){
				if(val== undefined){
					this.TemLabel=" "
				}else{			
					let index=this.temporaryLabel.indexOf(val);
					this.temporaryLabel.splice(index,1)			
					return;
				}			
				this.isLabel=false; 
			},
			submit(){
				let labelArray=[];  			
				if(this.TemLabel!=" "){
					labelArray.push(this.TemLabel);
				}
				if(this.temporaryLabel.length>0){
					console.log(this.temporaryLabel);
					labelArray.push.apply(labelArray,this.temporaryLabel);
				}
				alert(labelArray)
			}
		}
	}
</script>

样式我就不贴出来了,大家都懂

实现思路

第一个输入框input是写死的,第二个以上是通过遍历数组动态添加的

  • 点击“添加标签”弹出输入框,当只添加一个标签时,在提交的时候直接获取输入框内容
  • 当我们所添加的标签不只一个时,比如3个,那么除了第一个标签,剩下的2个就添加到
    一个数组中
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值