微信小程序如何实现通过js修改wxml的for循环中的属性值
2021/9/10 17:08:24
本文主要是介绍微信小程序如何实现通过js修改wxml的for循环中的属性值,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
微信小程序如何实现通过js修改wxml的for循环中的属性值
- 要实现的效果
- 具体代码
要实现的效果
点击每一个活动选项,实现显示对应的操作按钮
具体代码
首先要在对应页面的js中给data中定义数组,如下:
data: { vote_list: [{ 'vote_title': '活动1', 'vote_show': 'none' }, { 'vote_title': '活动2', 'vote_show': 'none' }, { 'vote_title': '活动3', 'vote_show': 'none' }], },
在wxml页面中循环显示
<view class="section" wx:for="{{vote_list}}" wx:key="index"> <view class="section_title" bindtap="voteAction" data-index="{{index}}">{{item.vote_title}}</view> <view class="section_title" style="display: {{item.vote_show}};"> <view class="section_title" bindtap="edit">编辑</view> <view class="section_title" bindtap="delete">删除</view> </view> </view>
在js中增加voteAction 绑定事件
voteAction: function (e) { console.log('voteAction-e', e); var index = e.currentTarget.dataset.index; var vote_list = this.data.vote_list; console.log('vote_show==', vote_list[index].vote_show); if (vote_list[index].vote_show == 'none') { vote_list[index].vote_show = 'block'; } else { vote_list[index].vote_show = 'none'; } this.setData({ vote_list: vote_list }) },
这篇关于微信小程序如何实现通过js修改wxml的for循环中的属性值的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-21Vue3教程:新手入门与实战指南
- 2024-09-21VueRouter4教程:入门与实践指南
- 2024-09-21Vuex4项目实战:新手入门教程
- 2024-09-21Vue3项目实战:初学者指南
- 2024-09-21Vue3项目实战:从零开始构建你的第一个Vue3应用
- 2024-09-21VueRouter4项目实战入门教程
- 2024-09-21Vue3入门:新手必读指南
- 2024-09-20Vue3教程:初学者快速入门指南
- 2024-09-20Vue资料入门教程:从零开始学习Vue.js
- 2024-09-20Vuex4入门教程:轻松掌握Vue状态管理