本文實(shí)例講述了微信小程序?qū)崿F(xiàn)點(diǎn)擊按鈕修改字體顏色功能。分享給大家供大家參考,具體如下:
1、效果展示
2、關(guān)鍵代碼
index.wxml文件
<view class="view" style="color:{{color}}">我是view標(biāo)簽</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}}"綁定事件動(dòng)態(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、源代碼點(diǎn)擊此處本站下載。
希望本文所述對(duì)大家微信小程序開(kāi)發(fā)有所幫助。