vue实现添加标签demo示例代码
作者:虎嗅添香 时间:2024-05-21 10:14:49
本篇文章主要介绍vue添加标签,废话不多说了,下面上具体代码
效果如下:
html
<div id="app">
<div style="margin-bottom: 4px;">
<span class="selectedItem" v-for="item in selectedItems">{{item.name}} <i class="red fa fa-close (alias)"
v-on:click="deleteSelectedItem($index)"></i></span>
<input v-model="inputItem" type="text" v-on:focus="showDropmenu" v-on:keyup.enter="addItem">
</div>
<div v-show="isShowDropmenu">
<button v-for="item in cataName" v-on:click="showCataList($index)">{{item.name}}</button>
<button v-on:click="hideDropmenu"><i class="red fa fa-close (alias)"></i></button>
<div v-for="item in cataList" v-show="item.isShow">
<span v-for="one in item.items" class="item" v-on:click="addByClick(one)">{{one}}</span>
</div>
</div>
</div>
js
new Vue({
el:'#app',
data:{
selectedItems: [{
name: 'NodeJs'
}],
isShowDropmenu: false,
inputItem:'',
cataName:[{name:'开发语法'}, {name: '系统设备'}],
cataList:[{
isShow: true,
items:['js','c++','java']
},{
isShow: false,
items:['windows','chrome','linux']
}]
},
methods:{
showDropmenu: function(event){
console.log('showDropmenu');
this.isShowDropmenu = true;
},
hideDropmenu: function(event){
this.isShowDropmenu = false;
console.log('hideDropmenu');
},
test: function(){
console.log('test');
},
addItem: function(){
this.selectedItems.push({name: this.inputItem});
this.inputItem = "";
},
deleteSelectedItem: function(index){
this.selectedItems.splice(index, 1);
},
showCataList: function(index){
var i = this.cataList.length;
while(i--){
i === index ? this.cataList[i].isShow = true: this.cataList[i].isShow = false;
}
},
addByClick: function(name){
var i = this.selectedItems.length;
while(i--){
if(this.selectedItems[i].name === name){
return;
}
}
this.selectedItems.push({name: name});
}
}
});
来源:https://segmentfault.com/a/1190000008170838
标签:vue,标签
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PyQt5每天必学之拖放事件
2021-02-28 19:26:15
![](https://img.aspxhome.com/file/2023/1/97191_0s.png)
将tf.batch_matmul替换成tf.matmul的实现
2021-03-25 03:35:43
![](https://img.aspxhome.com/file/2023/7/124787_0s.jpg)
使用python刷访问量的示例代码
2023-11-09 12:55:30
SQL Server 2008中的MERGE(不仅仅是合并)
2010-10-15 14:16:00
Python接口自动化之request请求封装源码分析
2023-12-29 02:42:32
使用python编写一个语音朗读闹钟功能的示例代码
2021-10-24 13:34:41
卸载安装Node.js与npm过程详解
2024-05-13 09:29:58
浅谈Python实现opencv之图片色素的数值运算和逻辑运算
2022-06-11 15:58:50
![](https://img.aspxhome.com/file/2023/4/107054_0s.png)
django 使用内置messages的操作
2023-04-23 13:35:56
Python入门_学会创建并调用函数的方法
2023-10-14 17:12:22
![](https://img.aspxhome.com/file/2023/4/66794_0s.jpg)
Python heapq使用详解及实例代码
2023-03-07 14:36:56
用非动态SQL Server SQL语句来对动态查询进行执行
2024-01-19 08:55:05
Python基础面向对象之继承与派生详解
2022-04-20 11:58:53
![](https://img.aspxhome.com/file/2023/4/67424_0s.png)
python3中的类继承你真的了解吗
2021-06-18 11:23:53
python中asyncio异步编程学习
2022-10-20 06:19:37
python基础之函数的返回值
2022-10-23 23:17:06
![](https://img.aspxhome.com/file/2023/4/105394_0s.jpg)
仿淘宝网站的导航标签效果!
2008-11-05 12:37:00
![](https://img.aspxhome.com/file/UploadPic/up/2008110512540282.gif)
利用Python的Django框架中的ORM建立查询API
2023-11-15 10:06:03
Python中使用OpenCV库来进行简单的气象学遥感影像计算
2021-02-02 09:45:49
![](https://img.aspxhome.com/file/2023/2/105742_0s.png)
Python3实现爬取简书首页文章标题和文章链接的方法【测试可用】
2021-10-08 00:22:10
![](https://img.aspxhome.com/file/2023/4/89334_0s.png)