东软学习,一个简易改良版计算器

<html xmlns="http://www.w3.org/1999/xhtml" lang="gb2312"> 
<head> 
<title>计算器</title>
</head> 
<script type="text/javascript" language="javascript">
function add(obj)
{
	var temp=obj.value;//目的是在进行过一次运算后在下次点击之前保留数值,防治出现摁两次情形
    document.getElementById("txt1").value+=obj.value;
	var obf=document.getElementById("txt1").value;
	 if(obf.indexOf("=")>0)//当input里面已经有过一次运算的痕迹之后进行清空
	 {
		 clears();
		 document.getElementById("txt1").value=temp;//将缓存起来的值吐出来
	 }
	 
}
function results()
{

    var obj=document.getElementById("txt1").value;//在此仍是字符串
    if(obj.indexOf("=")==-1)
        document.getElementById("txt1").value+="="+eval(obj);//eval函数可以经字符串转换成运算结果
}
function clears()
{
    document.getElementById("txt1").value="";
}
</script>
<body>
<table>
    <tr>
        <td><input type="button" οnclick="add(this)" id="Button1" value="1" /></td>
        <td><input type="button" οnclick="add(this)" id="Button2" value="2" /></td>
        <td><input type="button" οnclick="add(this)" id="Button3" value="3" /></td>
        <td><input type="button" οnclick="add(this)" id="Button13" value="+" /></td>
    </tr>
     <tr>
        <td><input type="button" οnclick="add(this)" id="Button4" value="4" /></td>
        <td><input type="button" οnclick="add(this)" id="Button5" value="5" /></td>
        <td><input type="button" οnclick="add(this)" id="Button6" value="6" /></td>
        <td><input type="button" οnclick="add(this)" id="Button14" value="-" /></td>
    </tr>
     <tr>
        <td><input type="button" οnclick="add(this)" id="Button7" value="7" /></td>
        <td><input type="button" οnclick="add(this)" id="Button8" value="8" /></td>
        <td><input type="button" οnclick="add(this)" id="Button9" value="9" /></td>
        <td><input type="button" οnclick="add(this)" id="Button15" value="*" /></td>
    </tr>
    <tr>
        <td><input type="button" οnclick="clears()" id="Button11" value="c" /></td>
        <td><input type="button" οnclick="add(this)" id="Button10" value="0" /></td>
        <td><input type="button" οnclick="results()" id="Button12" value="=" /></td>
        <td><input type="button" οnclick="add(this)" id="Button16" value="/" /></td>
    </tr>
</table>
<input type="text" id="txt1" />

</body>
</html>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值