JS计算器(自制)

<!doctype html>
<html>
<header>
<meta charset="utf-8">
<script src="jquery-1.9.1.min.js"></script>
<style>
button{
width:40px;
background-color:#2B91D5;
color:white;
}
input[type='text']{
width:98%;
}
</style>
</header>
<body onload="fun()">

<table border="1px">
<tr>
<td colspan="5"><input type="text" id="a" style="text-align:right;"></td>

</tr>
<tr>
<td><button class="num">9</button></td>
<td><button class="num">8</button></td>
<td><button class="num">7</button></td>
<td><button id="add">+</button></td>
<td><button id="c">C</button></td>

</tr>
<tr>

<td><button class="num">6</button></td>
<td><button class="num">5</button></td>
<td><button class="num">4</button></td>
<td><button id="j">-</button></td>
<td><button id="per">%</button></td>

</tr>
<tr>
<td><button class="num">1</button></td>
<td><button class="num">2</button></td>
<td><button class="num">3</button></td>
<td><button id="che">*</button></td>
<td><button id="d">=</button></td>
</tr>
<tr>
<td><button class="num">0</button></td>
<td><button class="bai">00</button></td>
<td><button class="dian">.</button></td>
<td><button id="chu">/</button></td>
<td><button id="wen">?</button></td>
</tr>
</table>

<div style="margin-left:20px;">







<input type="hidden" id="fh">
<input type="hidden" id="yl"><!--计算预留 -->
</div>
</body>
</html>
<script>
function fun(){
$("#a").val(0);
}
var a = $("#a").val();
var fh = $("#fh").val();
var yl = $("#yl").val();

$("#c").click(function(){
$("#a").val(0);
$("#yl").val("");
$("#fh").val("");
})
$(".num").click(function(){
var a = $("#a").val();
var fh = $("#fh").val();
var yl = $("#yl").val();
if(fh !== "" && a !== "" && yl !== ""){
num = this.innerHTML;
$("#a").val(a+num);
}else if(fh !== "" && yl == ""){

$("#yl").val($("#a").val());
$("#a").val(this.innerHTML);
}else{
num = this.innerHTML;
if($("#a").val()==0){
$("#a").val(num);
}else{
var a = $("#a").val();
$("#a").val(a+num);
}
}
})
$("#add").click(function(){
var a = $("#a").val();
$("#fh").val("+")

})
$("#j").click(function(){
var a = $("#a").val();
$("#fh").val("-")

})
$("#che").click(function(){
var a = $("#a").val();
$("#fh").val("*")

})
$("#chu").click(function(){
var a = $("#a").val();
$("#fh").val("/")

})
$("#d").click(function(){
var a = $("#a").val();
var fh = $("#fh").val();
var yl = $("#yl").val();
if(fh == "+"){

$("#a").val(Number(yl)+Number(a));
}else if(fh == "-"){

$("#a").val(Number(yl)-Number(a));
}
else if(fh == "*"){

$("#a").val(Number(yl)*Number(a));
}
else if(fh == "/"){

$("#a").val(Number(yl)/Number(a));
}
})
$(".bai").click(function fun(){
var a = $("#a").val();
$("#a").val(a+"00");
})
$(".dian").click(function fun(){
var a = $("#a").val();
$("#a").val(a+".");
})
$("#per").click(function fun(){
var a = $("#a").val();
$("#a").val(Number(a)/100);
})
$("#wen").click(function fun(){
alert("这个按钮也许没什么用?也许吧")
})
</script>

posted @ 2017-03-03 19:22  程昱仲德  阅读(159)  评论(0编辑  收藏  举报