JS计算器-修改后

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>计算器</title>
<style type="text/css">
input {
  cursor:hand;
  width:50px;
  height:38px;
}
body{
   background-image:url('beijing.gif');
   background-repeat:no-repeat; background-attachment: fixed;
}
#tab{
   margin-left:300px;
     border:1px solid #FF8000;
    
  
}
td{
  border:1px solid #FF8000;
}
</style>
</head>

<body >
 <br> <br> <br><br><br>
<table id="tab" width="300"   border=1px >
  <tr>
    <td colspan="4" height="40"><div id="text" align="right">0</div></td>
  </tr>
  <tr>
    <td height="30" width="60" align="center"><input type="button" name="1" id="1" value=" 1 " οnclick="getValue(this.id)"/></td>
    <td height="30" width="60" align="center"><input type="button" name="2" id="2" value=" 2 " οnclick="getValue(this.id)"/></td>
    <td height="30" width="60" align="center"><input type="button" name="3" id="3" value=" 3 " οnclick="getValue(this.id)"/></td>
    <td height="30" width="60" align="center"><input type="button" name="+" id="+" value=" + " οnclick="getfuhao(this.id)"/></td>
  </tr>
  <tr>
    <td height="30" width="60" align="center"><input type="button" name="4" id="4" value=" 4 " οnclick="getValue(this.id)"/></td>
    <td height="30" width="60" align="center"><input type="button" name="5" id="5" value=" 5 " οnclick="getValue(this.id)"/></td>
    <td height="30" width="60" align="center"><input type="button" name="6" id="6" value=" 6 " οnclick="getValue(this.id)"/></td>
    <td height="30" width="60" align="center"><input type="button" name="-" id="-" value=" - " οnclick="getfuhao(this.id)"/></td>
  </tr>
  <tr>
    <td height="30" width="60" align="center"><input type="button" name="7" id="7" value=" 7 " οnclick="getValue(this.id)"/></td>
    <td height="30" width="60" align="center"><input type="button" name="8" id="8" value=" 8 " οnclick="getValue(this.id)"/></td>
    <td height="30" width="60" align="center"><input type="button" name="9" id="9" value=" 9 " οnclick="getValue(this.id)"/></td>
    <td height="30" width="60" align="center"><input type="button" name="*" id="*" value=" * " οnclick="getfuhao(this.id)"/></td>
  </tr>
  <tr>
    <td height="30" width="60" align="center"><input type="button" name="C" id="C" value=" C " οnclick="C()"/></td>
    <td height="30" width="60" align="center"><input type="button" name="0" id="0" value=" 0 " οnclick="getValue(this.id)"/></td>
    <td height="30" width="60" align="center"><input type="button" name="=" id="=" value=" = "  οnclick="add()"/></td>
    <td height="30" width="60" align="center"><input type="button" name="/" id="/" value=" / " οnclick="getfuhao(this.id)"/></td>
  </tr>
</table>
<script language="javascript" charset="gb2312">
var str="";
var fuhao="";
function getValue(id)
{
str=str+id;
document.getElementById("text").innerHTML=str;
}
function getfuhao(id)
{
fuhao=id;
str=str+id;
document.getElementById("text").innerHTML=str;
}

function add()
{
 
var value=str.split(fuhao);
var s1=0;
var s2=0;
s1=s1+value[0]*1;
s2=s2+value[1]*1;
var value2;
if(fuhao=="+"){value2=s1+s2};
if(fuhao=="-"){value2=s1-s2};
if(fuhao=="*"){value2=s1*s2};
if(fuhao=="/"&&s2!=0){
  value2=s1/s2;
};
if(fuhao=="/"&&s2==0){
   alert("被除数不为0");
  C();
}else{
 document.getElementById("text").innerHTML=str+"="+value2;
 str=value2;
}

}

function C()
{
document.getElementById("text").innerHTML="0";
str="";
fuhao="";
}
</script>
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值