微信小程序?qū)崿F(xiàn)簡單計算器
更新時間:2021年04月12日 08:43:44 作者:m0_45972156
這篇文章主要為大家詳細介紹了微信小程序?qū)懞唵斡嬎闫?,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
微信小程序?qū)懙暮唵斡嬎闫?,供大家參考,具體內(nèi)容如下
jisaunqi.js
// pages/jisuanqi/jisuanqi.js Page({ /** * 頁面的初始數(shù)據(jù) */ data: { result:"0", string:"", cal:"", num1:"", num2:"" }, btSubmit:function(e){ console.log(e); var num1 = this.data.num1; var cal = this.data.cal; var num2 = this.data.num2; var char= e.target.id; var string ; if((char>="0"&&char<="9"||char=='.')&&cal==""){ num1=num1+char; this.setData({ num1:num1, }) } else if((char>="0"&&char<="9"||char=='.')&&cal!=""){ num2=num2+char; this.setData({ num2:num2, }) } else { cal=char; this.setData({ cal:cal, }) } this.setData({ string:num1+cal+num2 }) }, clean:function(e){ this.setData({ string:"", num1:"", num2:"", cal:"" }) }, calculate:function(e){ var num1 = this.data.num1; var num2 = this.data.num2; var cal = this.data.cal; var result; switch(cal){ case '+':result=num1*1+num2*1;break; case '-':result=num1*1-num2*1;break; case '*':result=(num1*1)*(num2*1);break; case '/':result=(num1*1)/(num2*1);break; } num1=result; cal=""; num2=""; this.setData({ result:result, num1:num1, cal:cal, num2:num2 }) }, reverse:function(e){ var cal = this.data.cal; var num1 = this.data.num1; var num2 = this.data.num2; if(cal==""){num1="-";} else if(cal!=""){num2="-"} this.setData({ num1:num1, num2:num2 }) }, lololo:function(e){ console.log(123) }, confirm:function(e){ console.log(555); console.log(e) }, event:function(e){ wx.navigateTo({ url: '/pages/event/event', }) }, bindinput:function(e){ console.log(1) }, jisuanqi:function(e){ var n1=e.detail.value.num1; var n2=e.detail.value.num2; var result=n1*1+n2*1; console.log(n1); console.log(n2); console.log(result); this.setData({ result:result }) }, tiaozhuan:function(e){ wx.navigateTo({ url: '/pages/9x9form/9x9form', }) }, /** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ onLoad: function (options) { }, /** * 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成 */ onReady: function () { }, /** * 生命周期函數(shù)--監(jiān)聽頁面顯示 */ onShow: function () { }, /** * 生命周期函數(shù)--監(jiān)聽頁面隱藏 */ onHide: function () { }, /** * 生命周期函數(shù)--監(jiān)聽頁面卸載 */ onUnload: function () { }, /** * 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動作 */ onPullDownRefresh: function () { }, /** * 頁面上拉觸底事件的處理函數(shù) */ onReachBottom: function () { }, /** * 用戶點擊右上角分享 */ onShareAppMessage: function () { } })
jisuanqi.json
{ "usingComponents": {}, "navigationBarTitleText": "計算器" }
jisuanqi.wxml
<view class="container"> <view class="view1">{{string}}</view> <view class="view2">{{result}}</view> <view class="button-group"> <button class="button">歷史</button> <button class="button" bindtap="clean">C</button> <button class="button"></button> <button class="button" id="/" bindtap="btSubmit">/</button> </view> <view class="button-group"> <button class="button" id="7" bindtap="btSubmit">7</button> <button class="button" id="8" bindtap="btSubmit">8</button> <button class="button" id="9" bindtap="btSubmit">9</button> <button class="button" id="*" bindtap="btSubmit">*</button> </view> <view class="button-group"> <button class="button" id="4" bindtap="btSubmit">4</button> <button class="button" id="5" bindtap="btSubmit">5</button> <button class="button" id="6" bindtap="btSubmit">6</button> <button class="button" id="-" bindtap="btSubmit">-</button> </view> <view class="button-group"> <button class="button" id="1" bindtap="btSubmit">1</button> <button class="button" id="2" bindtap="btSubmit">2</button> <button class="button" id="3" bindtap="btSubmit">3</button> <button class="button" id="+" bindtap="btSubmit">+</button> </view> <view class="button-group"> <button class="button" bindtap="reverse">-(符號減)</button> <button class="button" id="0" bindtap="btSubmit">0</button> <button class="button" id="." bindtap="btSubmit">.</button> <button class="button" bindtap="calculate">=</button> </view> </view> <navigator url="/pages/event/event">跳轉(zhuǎn)到event</navigator>//
jisuanqi.wxss
.button{ width: 160rpx; height: 100rpx; margin-left: 10rpx; padding-left: 10rpx; margin-top: 10rpx; text-align: center; line-height: 100rpx; padding: 5px; border-radius: 5px; } .button-group{ display: flex; flex-direction: row; align-content: flex-start; } .container{ display: flex; flex-direction: column; justify-content: flex-end; /* align-content: flex-end; */ } .view1{ height: 100rpx; background-color: #e4e4e4; line-height: 100rpx; font-size: 20px; } .view2{ height: 100rpx; margin-top: 5px; background-color: #e4e4e4; line-height: 100rpx; font-size: 20px; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript FAQ函數(shù)(提問+回復(fù))
javascript FAQ函數(shù),當點擊問題時顯示下面的回復(fù)內(nèi)容。2009-07-07javascript生成img標簽的3種實現(xiàn)方法(對象、方法、html)
這篇文章主要介紹了javascript生成img標簽的3種實現(xiàn)方法,包括對象、方法、html三種實現(xiàn)方式,具有一定參考借鑒價值,需要的朋友可以參考下2015-12-12javascript實現(xiàn)詳細時間提醒信息效果的方法
這篇文章主要介紹了javascript實現(xiàn)詳細時間提醒信息效果的方法,涉及javascript操作時間的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03