jQuery實(shí)現(xiàn)計(jì)算器功能
本文實(shí)例為大家分享了jQuery實(shí)現(xiàn)計(jì)算器功能的具體代碼,供大家參考,具體內(nèi)容如下
動(dòng)畫效果:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>計(jì)算器</title> <script src="../jquery.min.js"></script> <style> *{ margin: 0; padding: 0; } #calculator{ margin: 50px auto; padding: 5px; width: 230px; height: 230px; background: rgb(190,210,224); } #screen{ width: 230px; height: 60px; background: rgb(153,153,153); border-radius: 5px; text-align: right; overflow: hidden; } #txt1{ height: 20px; padding-top: 10px; font-size: 10px; } #txt2{ height: 30px; font-size: 20px; } #num{ float:left; width: 130px; } #num input{ width: 40px; height: 40px; margin-top: 3px; } #operator{ float:right; width: 70px; height: 170px; } #operator input{ width: 70px; height: 30px; margin-top: 4px ; } #converter{ float:right; width: 70px; height: 170px; } </style> </head> <body> <div id="calculator"> <div id="screen"> <p id="txt1"></p> <p id="txt2"></p> </div> <div id="workspace"> <div id="num"> <input type="button" value="7"> <input type="button" value="8"> <input type="button" value="9"> <input type="button" value="4"> <input type="button" value="5"> <input type="button" value="6"> <input type="button" value="1"> <input type="button" value="2"> <input type="button" value="3"> <input type="button" value="C"> <input type="button" value="0"> <input type="button" value="."> </div> <div id="operator"> <input type="button" value="+"> <input type="button" value="-"> <input type="button" value="*"> <input type="button" value="/"> <input type="button" value="="> </div> </div> </div> <script> $(function(){ var i=0;//i為清空標(biāo)志,i=1時(shí)需要清空#txt2的數(shù)據(jù) //獲取輸入的數(shù)字 $("#num input").click(function () { //先判斷#txt2中是否保存著上次計(jì)算的結(jié)果,如果是則將其清空 if (i===1){ $("#txt2").text(""); } //保證數(shù)字以正確的格式顯示 //使用switch語(yǔ)句實(shí)現(xiàn) switch ($(this).val()){ case "C": $("#txt2").text(""); break; case ".": if ($("#txt2").text().indexOf(".") != -1) { break; }else{$("#txt2").append($(this).val());} break; default: if ($("#txt2").text() === "0") { $("#txt2").text($(this).val()); }else{ $("#txt2").append($(this).val()); } } //使用if語(yǔ)句實(shí)現(xiàn) /* if ($(this).val()=="C"){ $("#txt2").text(" "); } else { if ($("#txt2").text().indexOf(".") != -1) { if ($(this).val() == ".") { } else { $("#txt2").append($(this).val()); } } else if ($("#txt2").text() === "0") { if ($(this).val() === ".") { $("#txt2").append($(this).val()); } else { $("#txt2").text($(this).val()); } }else{ $("#txt2").append($(this).val()); } }*/ i=0;//將清空標(biāo)志設(shè)為0 }); //獲取運(yùn)算符 $("#operator input:not(:last)").click(function () { $("#txt1").text($("#txt2").text()+$(this).val()); $("#txt2").text(""); }); //按下“=”鍵進(jìn)行計(jì)算 $("#operator input").last().click(function () { //使用eval()函數(shù) //$("#txt2").text(eval($("#txt1").text()+$("#txt2").text())); //使用常規(guī)方法 var str=$("#txt1").text(); var n1=parseFloat(str); var n2=parseFloat($("#txt2").text()); var cal=str[str.length-1]; switch (cal){ case "+": $("#txt2").text( n1+n2); break; case "-": $("#txt2").text( n1-n2); break; case "*": $("#txt2").text( n1*n2); break; case "/": $("#txt2").text( n1/n2); break; default: break; } $("#txt1").text( ""); i=1;//將清空標(biāo)志設(shè)為1 }); }); </script> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jQuery實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
- jQuery實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
- jquery實(shí)現(xiàn)計(jì)算器小功能
- jQuery實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- jQuery實(shí)現(xiàn)可以計(jì)算進(jìn)制轉(zhuǎn)換的計(jì)算器
- jQuery實(shí)現(xiàn)的簡(jiǎn)單在線計(jì)算器功能
- 基于HTML+CSS,jQuery編寫的簡(jiǎn)易計(jì)算器后續(xù)(添加了鍵盤監(jiān)聽)
- 一個(gè)簡(jiǎn)單的jQuery計(jì)算器實(shí)現(xiàn)了連續(xù)計(jì)算功能
- jQuery實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器
相關(guān)文章
實(shí)用的Jquery選項(xiàng)卡TAB示例代碼
Jquery選項(xiàng)卡想必大家并不陌生,本文為大家介紹個(gè)比較實(shí)用的jquery TAB選項(xiàng)卡,喜歡的朋友可以學(xué)習(xí)下2013-08-08jquery如何把數(shù)組變?yōu)樽址畟鞯椒?wù)端并處理
這篇文章主要介紹了jquery如何把數(shù)組變?yōu)樽址畟鞯椒?wù)端并處理,需要的朋友可以參考下2014-04-04基于jQuery實(shí)現(xiàn)多層次的手風(fēng)琴效果附源碼
今天我們要與大家分享一個(gè)漂亮而靈活的垂直 jQuery 手風(fēng)琴效果。其主要思想是擴(kuò)大手風(fēng)琴片上的點(diǎn)擊和顯示更多的信息。感興趣的朋友可以參考下本文2015-09-09jQuery DOM節(jié)點(diǎn)的遍歷方法小結(jié)
本篇文章主要介紹了jQuery DOM節(jié)點(diǎn)的遍歷方法小結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08jQuery通過(guò)deferred對(duì)象管理ajax異步
這篇文章主要介紹了jQuery通過(guò)deferred對(duì)象管理ajax異步的相關(guān)資料,需要的朋友可以參考下2016-05-05jQuery+css last-child實(shí)現(xiàn)選擇最后一個(gè)子元素操作示例
這篇文章主要介紹了jQuery+css last-child實(shí)現(xiàn)選擇最后一個(gè)子元素操作,結(jié)合實(shí)例形式分析了jQuery結(jié)合css進(jìn)行頁(yè)面元素選擇與樣式修改相關(guān)操作技巧,需要的朋友可以參考下2018-12-12用jQuery技術(shù)實(shí)現(xiàn)Tab頁(yè)界面之二
這個(gè)tab頁(yè)是把數(shù)據(jù)全部取回來(lái)再顯示,所以沒(méi)有數(shù)據(jù)緩存的特點(diǎn)。但是因?yàn)閿?shù)據(jù)全部是顯示的html代碼,所以對(duì)搜索引擎是友好的,也許對(duì)seo有好處。2009-09-09