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

JavaScript實現(xiàn)簡單計算器功能

 更新時間:2019年12月19日 15:25:39   作者:鄭德帥  
這篇文章主要為大家詳細介紹了JavaScript實現(xiàn)簡單計算器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了JavaScript實現(xiàn)簡單計算器的具體代碼,供大家參考,具體內(nèi)容如下

1.實現(xiàn)基本計算器功能,如圖

2.邏輯代碼

<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTF-8">
 <title>計算器</title>
 <!--設置樣式-->
 <style>
 .showdiv{
 text-align: center;
 margin:auto;/*設置居中*/
 border: solid 1px;
 width: 400px;
 height: 500px;
 border-radius: 10px;/*設置邊框角度*/
 }
 input[type="text"]{
 margin-top: 10px;
 width: 380px;
 height: 40px;
 font-size: 40px;
 }
 input[type="button"]{
 margin: 10px;
 margin-top: 20px;
 width: 60px;
 height: 80px;
 font-size: 40px;
 font-weight: bold;
 }
 </style>
 <!--設置js代碼-->
 <script type="text/javascript">
 /*將按鈕的值賦值給輸入框*/
 function num(btn){
 //把不能為零去掉
 if(document.getElementById("inp").value.match("除數(shù)")){
 document.getElementById("inp").value = "";
 }
 //獲取button按鈕的value
 var num = btn.value;
 console.log(num +" " +typeof(num))
 //將值賦值給text文本框
 switch(num){
 case "c":
 document.getElementById("inp").value = "";
 break;
 case "=":
 if(document.getElementById("inp").value.match("/")){
 if(document.getElementById("inp").value.split("/")[1] == "0"){
 document.getElementById("inp").value = "除數(shù)不能為零";
 }else{
 document.getElementById("inp").value = eval(document.getElementById("inp").value);
 }
 break;
 }else{
 document.getElementById("inp").value = eval(document.getElementById("inp").value);
 break;
 }
 default: 
 document.getElementById("inp").value = document.getElementById("inp").value+num;
 break;
 }
 }
 </script>
 </head>
 <body>
 <div class = "showdiv">
 <input type="text" name="" id="inp" value="" readonly="readonly"/><br />
 <input type="button" name="" id="" value="0" onclick="num(this)"/>
 <input type="button" name="" id="" value="1" onclick="num(this)"/>
 <input type="button" name="" id="" value="2" onclick="num(this)"/>
 <input type="button" name="" id="" value="3" onclick="num(this)"/><br />
 <input type="button" name="" id="" value="4" onclick="num(this)"/>
 <input type="button" name="" id="" value="5" onclick="num(this)"/>
 <input type="button" name="" id="" value="6" onclick="num(this)"/>
 <input type="button" name="" id="" value="7" onclick="num(this)"/><br />
 <input type="button" name="" id="" value="8" onclick="num(this)"/>
 <input type="button" name="" id="" value="9" onclick="num(this)"/>
 <input type="button" name="" id="" value="+" onclick="num(this)"/>
 <input type="button" name="" id="" value="-" onclick="num(this)"/><br />
 <input type="button" name="" id="" value="*" onclick="num(this)"/>
 <input type="button" name="" id="" value="/" onclick="num(this)"/>
 <input type="button" name="" id="" value="=" onclick="num(this)"/>
 <input type="button" name="" id="" value="c" onclick="num(this)"/>
 </div>
 </body>
</html>

關于計算器的精彩文章請查看《計算器專題》 ,更多精彩等你來發(fā)現(xiàn)!

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • localStorage實現(xiàn)便簽小程序

    localStorage實現(xiàn)便簽小程序

    這篇文章主要為大家詳細介紹了localStorage實現(xiàn)便簽小程序的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • 100個不能錯過的實用JS自定義函數(shù)

    100個不能錯過的實用JS自定義函數(shù)

    本文收集了100個原生態(tài)JavaScript編寫的常用、實用自定義函數(shù),需要的朋友可以參考下
    2014-03-03
  • 微信小程序API—獲取定位的詳解

    微信小程序API—獲取定位的詳解

    這篇文章主要介紹了微信小程序API獲取定位,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04
  • JS如何把字符串轉(zhuǎn)換成json

    JS如何把字符串轉(zhuǎn)換成json

    這篇文章主要介紹了JS如何把字符串轉(zhuǎn)換成json,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • Ext JS動態(tài)加載JavaScript創(chuàng)建窗體的方法

    Ext JS動態(tài)加載JavaScript創(chuàng)建窗體的方法

    這篇文章主要介紹了Ext JS動態(tài)加載JavaScript創(chuàng)建窗體的方法 ,需要的朋友可以參考下
    2016-06-06
  • 基于Web Audio API實現(xiàn)音頻可視化效果

    基于Web Audio API實現(xiàn)音頻可視化效果

    這篇文章主要介紹了基于Web Audio API實現(xiàn)音頻可視化效果,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • js實現(xiàn)簡易計算器小功能

    js實現(xiàn)簡易計算器小功能

    這篇文章主要為大家詳細介紹了js實現(xiàn)簡易計算器小功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • JavaScript仿微博輸入框效果(案例分析)

    JavaScript仿微博輸入框效果(案例分析)

    這篇文章給大家分享一個小的JavaScript的案例,就是模仿微博輸入框的效果,非常不錯,對微博輸入框效果感興趣的朋友通過本文學習吧
    2016-12-12
  • 微信小程序開發(fā)實現(xiàn)消息推送

    微信小程序開發(fā)實現(xiàn)消息推送

    這篇文章主要為大家詳細介紹了微信小程序開發(fā)實現(xiàn)消息推送,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • BootStrap便簽頁的簡單應用

    BootStrap便簽頁的簡單應用

    本文通過實例代碼給大家簡單介紹了bootstrap便簽頁的簡單應用,非常不錯,具有參考借鑒價值,需要的朋友參考下
    2017-01-01

最新評論

扶沟县| 即墨市| 宜兴市| 饶平县| 惠安县| 台南县| 潢川县| 盐源县| 和顺县| 泸定县| 鄂托克旗| 永嘉县| 黔江区| 密云县| 全椒县| 威海市| 北碚区| 康乐县| 通化市| 清镇市| 南郑县| 盐津县| 奇台县| 疏勒县| 探索| 大埔区| 金川县| 临洮县| 楚雄市| 桂阳县| 义乌市| 尚义县| 宕昌县| 南川市| 浦江县| 丹阳市| 大庆市| 米林县| 屏山县| 阿拉善左旗| 隆昌县|