微信小程序 點(diǎn)擊控件后選中其它反選實(shí)例詳解
微信小程序 點(diǎn)擊控件后選中其它反選實(shí)例詳解
前言:
如果需要實(shí)現(xiàn)進(jìn)來進(jìn)行給按鈕加上買一送一的樣式,或者單擊就選中單個按鈕,只能靠css結(jié)合js進(jìn)行控制了,小程序暫時沒有這樣的控件。
實(shí)現(xiàn)效果圖:
微信小程序進(jìn)來的時候自動進(jìn)行按鈕樣式的初始化,這個需要一個字段做判斷,加上正則表達(dá)式wxml文件:
<block wx:for="{{liuliangItems}}"> <block wx:if="{{item.one2one == 1}}"> <button class="{{item.changeColor?'selected2':'selected1'}}" type="default" id="{{item.price}}" data-id="{{item.name}}" data-one="{{item.one2one}}" data-orderid="{{item.id}}" data-number="1" bindtap="setPrice">{{item.name}}</button> </block> <block wx:else> <button class="{{item.changeColor?'selected':'normal'}}" type="default" id="{{item.price}}" data-id="{{item.name}}" data-one="{{item.one2one}}" data-orderid="{{item.id}}" data-number="1" bindtap="setPrice">{{item.name}}</button> </block> </block>
wxss文件
.normal{ box-sizing: border-box; flex: 0 0 21%; margin: 5px 5px; height: 50px; color:#1aad19; border:1px solid #1aad19; background-color:transparent; } .selected{ box-sizing: border-box; flex: 0 0 21%; margin: 5px 5px; height: 50px; background-color: #F75000; color: white; } .selected1{ box-sizing: border-box; flex: 0 0 21%; margin: 5px 5px; height: 50px; background-color: transparent; border:1px solid #1aad19; color:#1aad19; background-image: url(https://wxcx.llzt.net/images/hot.png) ; background-position:31px 0px; background-repeat:no-repeat; background-size:62%; } .selected2{ box-sizing: border-box; flex: 0 0 21%; margin: 5px 5px; height: 50px; background-color: #F75000; color: white; background-image: url(https://wxcx.llzt.net/images/hot.png); background-position:31px 0px; background-repeat:no-repeat; background-size:62%; }
現(xiàn)在的方法把集合進(jìn)行循環(huán),然后獲取當(dāng)前點(diǎn)擊的這個按鈕進(jìn)行比較,然后進(jìn)行樣式的修改
for (var i = 0; i < this.data.liuliangItems.length; i++) { if (e.target.dataset.orderid == this.data.liuliangItems[i].id) { txtArray1[i] = { id: this.data.liuliangItems[i].id, changeColor: true, price: this.data.liuliangItems[i].price, name: this.data.liuliangItems[i].name, one2one: this.data.liuliangItems[i].one2one } } else { txtArray1[i] = { id: this.data.liuliangItems[i].id, changeColor: false, price: this.data.liuliangItems[i].price, name: this.data.liuliangItems[i].name, one2one: this.data.liuliangItems[i].one2one } } }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- 微信小程序 實(shí)現(xiàn)點(diǎn)擊添加移除class
- 微信小程序?qū)崿F(xiàn)給循環(huán)列表添加點(diǎn)擊樣式實(shí)例
- 微信小程序?qū)崿F(xiàn)點(diǎn)擊按鈕修改view標(biāo)簽背景顏色功能示例【附demo源碼下載】
- 微信小程序?qū)崿F(xiàn)點(diǎn)擊按鈕修改字體顏色功能【附demo源碼下載】
- 微信小程序之綁定點(diǎn)擊事件實(shí)例詳解
- 微信小程序?qū)崿F(xiàn)點(diǎn)擊文字頁面跳轉(zhuǎn)功能【附源碼下載】
- 微信小程序加載更多 點(diǎn)擊查看更多
- 微信小程序點(diǎn)擊控件修改樣式實(shí)例詳解
- 微信小程序?qū)崿F(xiàn)點(diǎn)擊按鈕修改文字大小功能【附demo源碼下載】
- 微信小程序?qū)崿F(xiàn)點(diǎn)擊效果
相關(guān)文章
Javascript面象對象成員、共享成員變量實(shí)驗(yàn)
Javascript 面象對象成員、共享成員變量實(shí)驗(yàn),需要的朋友可以參考下。2010-11-11js以對象為索引的關(guān)聯(lián)數(shù)組
在代碼邏輯中更多的是用關(guān)聯(lián)數(shù)組的方式。但即使是這樣我們也很少使用對象類型作為鍵值對的鍵名。2010-07-07微信小程序?qū)崿F(xiàn)搜索指定景點(diǎn)周邊美食、酒店
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)搜索指定景點(diǎn)周邊美食、酒店的功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05javascript實(shí)現(xiàn)數(shù)字時鐘特效
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)數(shù)字時鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09