jQuery實(shí)現(xiàn)計(jì)算器功能
更新時(shí)間:2020年10月19日 11:47:33 作者:willard_cui
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(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語句實(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語句實(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>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- jQuery實(shí)現(xiàn)簡易計(jì)算器功能
- jQuery實(shí)現(xiàn)簡單計(jì)算器
- jquery實(shí)現(xiàn)計(jì)算器小功能
- jQuery實(shí)現(xiàn)簡單計(jì)算器功能
- jQuery實(shí)現(xiàn)可以計(jì)算進(jìn)制轉(zhuǎn)換的計(jì)算器
- jQuery實(shí)現(xiàn)的簡單在線計(jì)算器功能
- 基于HTML+CSS,jQuery編寫的簡易計(jì)算器后續(xù)(添加了鍵盤監(jiān)聽)
- 一個(gè)簡單的jQuery計(jì)算器實(shí)現(xiàn)了連續(xù)計(jì)算功能
- jQuery實(shí)現(xiàn)簡易的計(jì)算器
相關(guān)文章
jquery實(shí)現(xiàn)有過渡效果的tab切換
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)有過渡效果的tab切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
基于jQuery Bar Indicator 插件實(shí)現(xiàn)進(jìn)度條展示效果
這篇文章主要介紹了基于jQuery Bar Indicator 插件實(shí)現(xiàn)進(jìn)度條展示效果的相關(guān)資料,需要的朋友可以參考下2015-09-09
jquery實(shí)現(xiàn)人性化的有選擇性禁用鼠標(biāo)右鍵
與其使用比較暴力的手段禁用鼠標(biāo)右鍵,還不如有選擇性人性化的的禁用鼠標(biāo)右鍵,需要的朋友可以參考下2014-06-06
Javascript 使用ajax與C#獲取文件大小實(shí)例詳解
本文章向碼農(nóng)們介紹了js ajax獲取文件大小的實(shí)例,涉及JavaScript調(diào)用ajax交互及后臺(tái)C#文件操作的相關(guān)技巧,需要的朋友可以參考下2017-01-01

