uniapp,点击选中并改变颜色,第二次点击取消选中状态

一、效果图

在这里插入图片描述

二、代码实现

字符串的indexOf和数组的indexOf用法一致!

arr.indexOf(item) 该方法返回某个元素在数组中的位置。若没检索到,则返回 -1。

关键代码:(通过:class绑定)
:class="selectList.indexOf(sub.type) !== -1 ? 'right_item right_item_active':'right_item'"

2.1html

		<view class="right">
			<!-- 通过(selectList.indexOf(sub.type) !== -1)来判断选中的数组里,是否有该元素。
				点击时,如果有,会移除;如果没有,会添加上 
			-->
			<view :class="selectList.indexOf(sub.type) !== -1 ? 'right_item right_item_active':'right_item'"
				v-for="(sub,index) in leftList[tabindex].children" :key="index" @click="rightBtn(sub,index)">
				{
   {
    sub.type }}
			</view>
		</view>

2.2js

			//点击右侧
			rightBtn(sub, index) {
   
				console.log(sub, index)
				this.rightindex = index;

				//uniapp 数组添加不重复元素
				if (this.selectList.includes(sub.type)) {
   
					this.selectList = this.selectList.filter((item) => {
   
						return item != sub.type;
					});
				} else {
   
					this.selectList.push(sub.type);
					this.selectList = [...new Set(this.selectList)]; // 数组去重
					if (this.selectList.length > 3) {
   
						uni.showToast({
   
							title: "最多选3个",
							icon: "none"
						});
						this.selectList.splice(3, this.selectList.length - 3);
					}
				}
			},

完成~

相关推荐

  1. 菜单改变svg图标颜色

    2023-12-10 01:12:01       11 阅读
  2. Uniapp 图片放大

    2023-12-10 01:12:01       16 阅读
  3. uniapp实现选项跳转到应用商店进行下载

    2023-12-10 01:12:01       24 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-10 01:12:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-10 01:12:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-10 01:12:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-10 01:12:01       20 阅读

热门阅读

  1. spring 两个service相互引用,会有循环依赖吗

    2023-12-10 01:12:01       33 阅读
  2. Lintcode 1160 · Campus Bikes (三元组排序好题)

    2023-12-10 01:12:01       27 阅读
  3. ca单点登录

    2023-12-10 01:12:01       40 阅读
  4. 半导体材料

    2023-12-10 01:12:01       38 阅读
  5. 枚举类简单使用

    2023-12-10 01:12:01       37 阅读
  6. mysql数据库查询语句配例题(一)

    2023-12-10 01:12:01       31 阅读
  7. ActiveMQ断线重连技巧,即通信高可用的配置

    2023-12-10 01:12:01       34 阅读
  8. unexpected token .bug报错

    2023-12-10 01:12:01       40 阅读
  9. 删除Windows系统中无用的隐藏设备

    2023-12-10 01:12:01       32 阅读