小程序:wx:for里面的item,index。以及自定义属性data-xxx的用法(双层循环取下标)。

1.定义

通俗来说,

wx:for    这个是正常的循环,没什么可说的。

wx:for-item=‘变量名(随便起的)’   它是指定循环数据当前的变量名,可以通过  {{变量名.属性}} 展示数组的元素。

wx:for-index=‘变量名(随便起)’,它是指向当前元素的下标名,可以在其他事件中定义自定义事件(data-xxx='{{变量名}}',,该自定义属性可以在参数e下面打印出来)获取该下标。

2.用法。

wx:for-item用法。

< view class = 'content' wx:for = '{{ item.product_list }}' wx:key = 'index' wx:for-index = "bindex" wx:for-item= 'product_list'>
< view class= 'content_left'>
< image class= 'select_img' src= "{{isSelect?'/img/check_select.png':'/img/check_btn.png'}}" bindtap= 'switchSelect' data-aindex= '{{aindex}}' data-bindex= '{{bindex}}'></ image >
< view class= 'product_img'>
< image src= '/img/banner.gif'></ image >
< text class = 'product_title'> {{ product_list.spac }}< / text >
</ view >
</ view >
定义名称,使用时跟对象一样。

wx:for-index用法。

< view class = 'content' wx:for = '{{ item.product_list }}' wx:key = 'index' wx:for-index="bindex" wx:for-item = 'product_list'>
< view class= 'content_left'>
< image class= 'select_img' src= "{{isSelect?'/img/check_select.png':'/img/check_btn.png'}}" bindtap= 'switchSelect' data-aindex= '{{aindex}}'
                     data-bindex= '{{bindex}}'></ image >
上边定义完之后,下面可以通过自定义属性获取该下标,并可通过e.xx进行数据的获取。

数据的获取:



  • 23
    点赞
  • 92
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
微信小程序中,使用`data-`属性可以在`wx:for`场景下传递事件参数。具体实现方法如下: 1. 在`wx:for`中使用`template`标签定义模板。 2. 在模板定义中使用`data-`属性传递需要传递的事件参数。 ```html <template name="list-item"> <view class="list-item" data-index="{{index}}" data-item="{{item}}" bindtap="onListItemTap"> <text>{{item}}</text> </view> </template> ``` 在上面的代码中,我们在模板定义中使用了`data-index`和`data-item`两个`data-`属性,分别传递了列表项的索引和值。 3. 在`wx:for`中使用模板并传入数据。 ```html <view class="list"> <template wx:for="{{list}}" wx:for-item="item"> <template is="list-item" data="{{index: {{index}}, item: {{item}}}}"></template> </template> </view> ``` 在上面的代码中,我们使用`wx:for`循环渲染`list`数组,然后在循环体中使用`template`标签引入之前定义的模板,并通过`data`属性将需要传递的事件参数传入。 4. 在事件处理函数中获传递的参数。 ```javascript Page({ onListItemTap: function (event) { const index = event.currentTarget.dataset.index; const item = event.currentTarget.dataset.item; console.log(`点击了列表项${index},值为${item}`); }, }); ``` 在上面的代码中,我们在事件处理函数中通过`event.currentTarget.dataset`获到传递的`data-`属性,然后获到需要的参数值。 至此,在`wx:for`场景下使用`data-`属性传递事件参数的方法就介绍完了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值