小程序循环时点击单/多个显示对应的样式

循环时点击单个显示对应样式

效果图

在这里插入图片描述

代码

wxml

<view class="main">
    <!-- 首先获取对应的ID值 进行判断点击 -->
    <view class="{{currindex==item.id ? 'header1':'header'}}" 
    data-id="{{item.id}}" wx:for="{{list}}" wx:key="id" bindtap="btnindex">
        {{item.name}}
    </view>
</view>
  

wxss

.header{
    width: 100px;
    height: 40px;
    line-height: 40px;
    border: 1px solid black;
    margin-bottom: 10px;
    text-align: center;
}
.header1{
    width: 100px;
    height: 40px;
    line-height: 40px;
    border: 2px solid red;
    margin-bottom: 10px;
    text-align: center;
}

js

// pages/2/2.js
Page({

        /**
         * 页面的初始数据
         */
        data: {
                list: [
                        {
                                "name": "Henry",
                                "phone": "333-444-555",
                                "email": "henry@email.com",
                                "id": 1,
                                "age": 33,
                                "companyId": 1
                        }, {
                                "name": "Bucky",
                                "phone": "333-444-555",
                                "email": "bucky@email.com",
                                "id": 2,
                                "age": 34,
                                "companyId": 2
                        }, {
                                "name": "Emily",
                                "phone": "333-444-555",
                                "email": "emily@email.com",
                                "id": 3,
                                "age": 43,
                                "companyId": 3
                        }
                ],
                currindex: -1,//定义一个值进行赋值,在点击时显示对应的样式
        },
        btnindex(e) {
                console.log(e)
                this.setData({
                        currindex: e.currentTarget.dataset.id
                })
        },
    
})

循环时点击显示样式对应的图片显示与隐藏

效果图

在这里插入图片描述综上所述:
在js的data里面定义一个方法显示为true

 centerimg: true

然后根据上面你所定义的ID值进行判断
这边的判断和上面点击显示对应的样式大差不差
在这里插入图片描述

循环时点击多个显示对应的样式

代写-----------------------------------------------

效果图

代码

wxml

在这里插入代码片

wxss

在这里插入代码片

js

在这里插入代码片

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值