本文實例講述了微信小程序實現(xiàn)點擊按鈕修改字體顏色功能。分享給大家供大家參考,具體如下:
1、效果展示
2、關鍵代碼
index.wxml文件
<view class="view" style="color:{{color}}">我是view標簽</view> <view style="display:flex;"> <block wx:for="{{colorArray}}" wx:key="" wx:for-item="Color"> <button class="btn" style="background:{{Color}};" type="default" bindtap="bindtap{{index}}"></button> </block> </view>
這里使用bindtap="bindtap{{index}}"綁定事件動態(tài)修改style="color:{{color}}"中的顏色值。
index.js文件
var pageData={} pageData.data={ color:'black', colorArray:['red','blue','yellow','green','black'] } for(var i=0;i<5;++i){ (function(index){ pageData['bindtap'+index]=function(e){ this.setData({ color:this.data.colorArray[index] }) } })(i) } Page(pageData)
3、源代碼點擊此處本站下載。
希望本文所述對大家微信小程序開發(fā)有所幫助。