最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

微信小程序?qū)崿F(xiàn)簡(jiǎn)單的計(jì)算器功能

 更新時(shí)間:2021年07月19日 09:03:17   作者:聰明可愛(ài)小軒軒  
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)簡(jiǎn)單的計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)計(jì)算器功能的具體代碼,供大家參考,具體內(nèi)容如下

wxml

<view class='content'>
  <input value='{{calculation}}'></input>
  <view class='box'>
    <button class='yellow-color'>退格</button>
    <button class='yellow-color' bindtap='empty'>清屏</button>
    <button class='yellow-color'>❤</button>
    <button bindtap='add' data-text='+' class='yellow-color'>+</button>
  </view>

  <view class='box'>
    <button bindtap='add' data-text='9'>9</button>
    <button bindtap='add' data-text='8'>8</button>
    <button bindtap='add' data-text='7'>7</button>
    <button bindtap='add' class='yellow-color' data-text='-'>-</button>
  </view>

  <view class='box'>
    <button bindtap='add' data-text='6'>6</button>
    <button bindtap='add' data-text='5'>5</button>
    <button bindtap='add' data-text='4'>4</button>
    <button bindtap='add' class='yellow-color' data-text='*'>*</button>
  </view>

  <view class='box'>
    <button bindtap='add' data-text='3'>3</button>
    <button bindtap='add' data-text='2'>2</button>
    <button bindtap='add' data-text='1'>1</button>
    <button bindtap='add' data-text='/' class='yellow-color'>÷</button>
  </view>

  <view class='box'>
    <button bindtap='add' data-text='0'>0</button>
    <button bindtap='add' data-text='.'>.</button>
    <button>歷史</button>
    <button class='yellow-color' bindtap='res'>=</button>
  </view>


</view>

wxss

input {
  width: 95%;
  height: 250rpx;
  margin: 0 auto;
  margin-bottom: 20rpx;
  border-bottom: 1rpx solid #ccc;
}

.box {
  display: flex;
}
button {
  width: 20%;
  height: 150rpx;
  margin-bottom: 20rpx;
  line-height: 150rpx;
  background-color:rgb(0, 150, 250);
  color: white;
}

.yellow-color {
  background-color: rgb(247, 142, 24)
}

JS

//index.js
//獲取應(yīng)用實(shí)例
const app = getApp()

Page({
  data: {
    calculation:"",
    result:0,
    character:[],  // 運(yùn)算符號(hào)
    operand: [],    // 數(shù)字
    temp:false
  },

  // 輸入框輸入數(shù)據(jù)
  add:function(e) {
    let input = e.currentTarget.dataset.text;
    var that = this;
    if (input == '+' || input == '-' || input == '*' || input == '/') {
      this.data.temp = false; // 用于記錄上一次是否是操作符
      var item = 'character[' + this.data.character.length+ ']';
      this.setData({
        [item] :input
      }) 
    } else {
      var item = 'operand['+this.data.operand.length+']';
     
      if(that.data.temp) {
        // 拿到前一個(gè)的值
        var res = 'operand[' + (this.data.operand.length-1) + ']'
        
        var ress= that.data.operand.length-1;
        var xyz = that.data.operand[ress] * 10 + parseInt(input);
        that.setData({
          [res]:xyz
        })
      } else {
        input = parseInt(input);
        that.data.temp = true;
        that.setData({
          [item]: input
        })
      }
    }
    // 將所有的內(nèi)容放到顯示框中
    this.setData({
      calculation:this.data.calculation+input
    })

  },

  // 計(jì)算答案
  res:function() {
    console.log(this.data.character.length);
    console.log(this.data.operand.length)
    var character_len =  this.data.character.length ;
    var operand_len = this.data.operand.length;
    console.log(operand_len - character_len)
    if(operand_len - character_len == 1) {
      this.data.result = this.data.operand[0];
      console.log("初始值"+this.data.result);
      for(var i=0;i<character_len;i++) {
        if(this.data.character[i] == '+') {
          this.data.result = this.data.result + this.data.operand[i + 1];

        }
        if (this.data.character[i] == '-') {
          this.data.result = this.data.result - this.data.operand[i + 1];

        }
        if (this.data.character[i] == '*') {
          this.data.result = this.data.result * this.data.operand[i + 1];

        }
        if (this.data.character[i] == '/') {
          this.data.result = this.data.result / this.data.operand[i + 1];

        }
        
      }
    } else {
      this.setData({
        result:'輸入有誤,清空數(shù)據(jù),重新輸入'
      })
    }

    this.setData({
     calculation:this.data.result
    })
  },

  // 清空屏幕
  empty:function() {
    this.setData({
      calculation: "",
      result: 0,
      character: [],  // 運(yùn)算符號(hào)
      operand: [],    // 數(shù)字
      temp: false
    }
  }
})

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解webpack-dev-server使用方法

    詳解webpack-dev-server使用方法

    這篇文章主要介紹了詳解webpack-dev-server使用方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • JavaScript位移運(yùn)算符(無(wú)符號(hào)) >>> 三個(gè)大于號(hào) 的使用方法詳解

    JavaScript位移運(yùn)算符(無(wú)符號(hào)) >>> 三個(gè)大于號(hào) 的使用方法詳解

    這篇文章主要介紹了JavaScript位移運(yùn)算符(無(wú)符號(hào)) >>> 三個(gè)大于號(hào) 的使用方法詳解的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • 基于JavaScript實(shí)現(xiàn)自動(dòng)更新倒計(jì)時(shí)效果

    基于JavaScript實(shí)現(xiàn)自動(dòng)更新倒計(jì)時(shí)效果

    這篇文章主要為大家詳細(xì)介紹了基于JavaScript實(shí)現(xiàn)自動(dòng)更新倒計(jì)時(shí)效果,元旦倒計(jì)時(shí)代碼分享,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • JavaScript中的原型和繼承詳解(圖文)

    JavaScript中的原型和繼承詳解(圖文)

    到現(xiàn)在,我們就有討論 JavaScript 中的原型和繼承問(wèn)題的基礎(chǔ)了。它雖然并不像你在 C++、Java 或 C# 中了解的經(jīng)典繼承模式一樣,但這種方式同樣強(qiáng)大,并且有可能會(huì)更加靈活
    2014-07-07
  • 詳解JavaScript的內(nèi)存空間、賦值和深淺拷貝

    詳解JavaScript的內(nèi)存空間、賦值和深淺拷貝

    這篇文章主要介紹了JavaScript的內(nèi)存空間、賦值和深淺拷貝,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • JavaScript 學(xué)習(xí)筆記(四)

    JavaScript 學(xué)習(xí)筆記(四)

    今天繼續(xù)學(xué)習(xí)JS中的對(duì)象,昨天學(xué)完了本地對(duì)象中的兩個(gè)重要對(duì)象Array和Date。今天看下內(nèi)置對(duì)象Global對(duì)象和Math對(duì)象。
    2009-12-12
  • cordova入門基礎(chǔ)教程及使用中遇到的一些問(wèn)題總結(jié)

    cordova入門基礎(chǔ)教程及使用中遇到的一些問(wèn)題總結(jié)

    這篇文章主要給大家介紹了關(guān)于cordova的入門基礎(chǔ)教程以及在使用中遇到的一些問(wèn)題,文中通過(guò)示例代碼一步步介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • cropper js基于vue的圖片裁剪上傳功能的實(shí)現(xiàn)代碼

    cropper js基于vue的圖片裁剪上傳功能的實(shí)現(xiàn)代碼

    這篇文章主要介紹了cropper js基于vue的圖片裁剪上傳功能的相關(guān)資料,需要的朋友可以參考下
    2018-03-03
  • 讓JavaScript 輕松支持函數(shù)重載 (Part 1 - 設(shè)計(jì))

    讓JavaScript 輕松支持函數(shù)重載 (Part 1 - 設(shè)計(jì))

    JavaScript支持函數(shù)重載嗎?可以說(shuō)不支持,也可以說(shuō)支持。說(shuō)不支持,是因?yàn)镴avaScript不能好像其它原生支持函數(shù)重載的語(yǔ)言一樣,直接寫多個(gè)同名函數(shù),讓編譯器來(lái)判斷某個(gè)調(diào)用對(duì)應(yīng)的是哪一個(gè)重載。
    2009-08-08
  • LayUI樹形表格treetable使用及說(shuō)明

    LayUI樹形表格treetable使用及說(shuō)明

    這篇文章主要介紹了LayUI樹形表格treetable使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10

最新評(píng)論

兴城市| 修武县| 潞西市| 清丰县| 图们市| 西贡区| 浠水县| 大荔县| 新营市| 正安县| 蒙阴县| 元谋县| 鄢陵县| 达日县| 桑植县| 岗巴县| 綦江县| 尤溪县| 城口县| 长兴县| 石棉县| 敦化市| 赣榆县| 应城市| 柳河县| 海门市| 康定县| 且末县| 竹北市| 张家川| 静乐县| 宣化县| 郧西县| 门源| 吉安市| 丹江口市| 奇台县| 南充市| 犍为县| 五莲县| 罗甸县|