代码如下:
/----------wxml------------/
<view class="container">
<button bindtap="change" >点击切换</button>
<view class='{{index=="true"?"hide":"present"}}' style="background-color:red;width:200rpx;height:200rpx;margin:20px 0 30px 0;"></view>
二、点击关毕
三、再点击就出不来?怎么回事,求教大神
网友回复:
是false
不是fales
this.setData=({index:'fales'})
这句写错了吧,怎么多个=号出来?删掉就正常了
2L正解
2L的 , 我纠正那个错误了 ,你再试下能行?
我发现,无论点多少次,setData()只执行一次,就是第一次点击时执行,往后setData()不再执行了!
这里请问的是,要实现这个功能我应该怎写才对呢
你可以试试用条件渲染
当把button换成switch后,为什么就出问题了
代码如下:
/------------------js--------------------/
var app = getApp()
Page({
data: {
index: 'false',
},
change: function (e) {
var d=e.detail.value;
console.log(d);
if (this.data.index == 'false') {
this.setData({ index: d })
} else {
this.setData({ index: d })
}; console.log(this.data.index);
}
})
/----------------wxml-----------------/
<switch type="switch" bindchange="change" style="margin-top:20px;" />
<view class='{{index=="true"?"hide":"present"}}' style="background-color:red;width:200rpx;height:200rpx;margin-top:20px;"></view>
当把class='{{index=="true"?"hide":"present"}}'换成class='{{index=="false"?"present":"hide"}}'后,效果只能出现一次
<view class="{{index == true ?'hide':'present'}}"
引号的问题,这样就可以了
你有试过吗,你这样写效果一次都出不来
/------------------js--------------------/
var app = getApp()
Page({
data: {
index: false,
},
change: function (e) {
var d = e.detail.value;
this.setData({ index: d })
}
})
/----------------wxml-----------------/
<view class='{{!index?"hide":"present"}}' style="background-color:red;width:200rpx;height:200rpx;margin-top:20px;"></view>
已经测试过,能正常运行。