用js实现简单的计算器
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{ num2=document.getElementById("txt").value; document.getElementById("txt").value=Number(num1)+Number(num2) ;}
if(index=="-")
{ num2=document.getElementById("txt").value; document.getElementById("txt").value=Number(num1)-Number(num2); }
if("/"==p)
{ num1=document.getElementById("txt").value;
index="/"; document.getElementById("txt").value=""; }
else if("="==p){ //判断运行符号,进行值的设定
if(index=="+")
<input type="button" value="0" id="zero" onClick="getCount(0)">
<input type="button" value="." id="point" onClick="getCount('.')">
<input type="button" value="=" id="dengyu" onClick=" yunsuan('=')">
}
if("-"==p)
{ num1=document.getElementById("txt").value; index="-"; document.getElementById("txt").value=""; }
if("*"==p)
{ num1=document.getElementById("txt").value; index="*";document.getElementById("txt").value=""; }
<input type="button" value="/" id="chu" onClick="yunsuan('/')">
<input type="button" value="4" id="four" onClick="getCount(4)">
<input type="button" value="5" id="five" onClick="getCount(5)">
<input type="button" value="2" id="two" onClick="getCount(2)">
<input type="button" value="3" id="three" onClick="getCount(3)">
<input type="button" value="-" id="jian" onClick="yunsuan('-')" >
<bБайду номын сангаасdy>
<div class="jsj">
<input type="text" id="txt" value="0" style="width:225;height:50;text-align:right">
<input type="button" id="clear" value="C" onClick="clearOut()">
<html>
<head>
<style>
.jsj{
width: 240px;
}
.jsj input{
width: 50px;
height: 50px;
margin-top: 10px;
}
#pm{
width: 220px;
}
</style>
<script language="javascript">
var num1;
if(index=="*")
{ num2=document.getElementById("txt").value; document.getElementById("txt").value=Number(num1)*Number(num2); }
if(index=="/")
{ num2=document.getElementById("txt").value; document.getElementById("txt").value=Number(num1)/Number(num2); } } } </script> </head>
<input type="button" value="7" id="seven" onClick="getCount(7)">
<input type="button" value="8" id="eight" onClick="getCount(8)">
<input type="button" value="9" id="nine" onClick="getCount(9)">
<input type="button" value="+" id="jia" onClick="yunsuan('+')">
</div>
</body>
</html>
<input type="button" value="6" id="six" onClick="getCount(6)">
<input type="button" value="*" id="cheng" onClick="yunsuan('*')">
<input type="button" value="1" id="one" onClick="getCount(1)">
var num2;
var index;
function clearOut(){
document.getElementById("txt").value=0; }
function getCount(count){
var num=document.getElementById("txt").value;
if(num==0){num="";}
document.getElementById("txt").value=num+count; }
function yunsuan(p)
{ if("+"==p)
{ num1=document.getElementById("txt").value;
index="+"; document.getElementById("txt").value="";
if(index=="-")
{ num2=document.getElementById("txt").value; document.getElementById("txt").value=Number(num1)-Number(num2); }
if("/"==p)
{ num1=document.getElementById("txt").value;
index="/"; document.getElementById("txt").value=""; }
else if("="==p){ //判断运行符号,进行值的设定
if(index=="+")
<input type="button" value="0" id="zero" onClick="getCount(0)">
<input type="button" value="." id="point" onClick="getCount('.')">
<input type="button" value="=" id="dengyu" onClick=" yunsuan('=')">
}
if("-"==p)
{ num1=document.getElementById("txt").value; index="-"; document.getElementById("txt").value=""; }
if("*"==p)
{ num1=document.getElementById("txt").value; index="*";document.getElementById("txt").value=""; }
<input type="button" value="/" id="chu" onClick="yunsuan('/')">
<input type="button" value="4" id="four" onClick="getCount(4)">
<input type="button" value="5" id="five" onClick="getCount(5)">
<input type="button" value="2" id="two" onClick="getCount(2)">
<input type="button" value="3" id="three" onClick="getCount(3)">
<input type="button" value="-" id="jian" onClick="yunsuan('-')" >
<bБайду номын сангаасdy>
<div class="jsj">
<input type="text" id="txt" value="0" style="width:225;height:50;text-align:right">
<input type="button" id="clear" value="C" onClick="clearOut()">
<html>
<head>
<style>
.jsj{
width: 240px;
}
.jsj input{
width: 50px;
height: 50px;
margin-top: 10px;
}
#pm{
width: 220px;
}
</style>
<script language="javascript">
var num1;
if(index=="*")
{ num2=document.getElementById("txt").value; document.getElementById("txt").value=Number(num1)*Number(num2); }
if(index=="/")
{ num2=document.getElementById("txt").value; document.getElementById("txt").value=Number(num1)/Number(num2); } } } </script> </head>
<input type="button" value="7" id="seven" onClick="getCount(7)">
<input type="button" value="8" id="eight" onClick="getCount(8)">
<input type="button" value="9" id="nine" onClick="getCount(9)">
<input type="button" value="+" id="jia" onClick="yunsuan('+')">
</div>
</body>
</html>
<input type="button" value="6" id="six" onClick="getCount(6)">
<input type="button" value="*" id="cheng" onClick="yunsuan('*')">
<input type="button" value="1" id="one" onClick="getCount(1)">
var num2;
var index;
function clearOut(){
document.getElementById("txt").value=0; }
function getCount(count){
var num=document.getElementById("txt").value;
if(num==0){num="";}
document.getElementById("txt").value=num+count; }
function yunsuan(p)
{ if("+"==p)
{ num1=document.getElementById("txt").value;
index="+"; document.getElementById("txt").value="";