有缺陷欢迎指导
<!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=utf-8" />
<title>无标题文档</title>
<script>
var num1="";
var num2="";
var oper = "";
function test(num){
var initData="";
initData=document.getElementById("t1").value;
initData+=num;
document.getElementById("t1").value = initData;
}//给文本框赋值
function op(op_v){
num1 = document.getElementById("t1").value;
oper = op_v;
document.getElementById("t1").value="";
}
function ev() {
num2 = document.getElementById("t1").value;
switch(oper){
case '+':
document.getElementById("t1").value= num1*1+num2*1;
break;
case '-':
document.getElementById("t1").value= num1*1-num2*1;
break;
case '*':
document.getElementById("t1").value= ((num1*1)*(num2*1));
break;
case '/':
document.getElementById("t1").value= num1*1/num2*1;
break;
}
}
function comp(){
document.getElementById("t1").value=document.getElementById("t1").value*(-1);
}
//document.getElementById("t1").value = eval(document.getElementById("t1").value); } //计算文本框内的表达式
function clear1() {
document.getElementById("t1").value="";
} //吧文本框内的内容清0
function del() {
var i=document.getElementById("t1").value;
i=i.substring(0,i.length-1)
document.getElementById("t1").value=i;
} //删除 .
</script>
<style>
input {
width:80px;
height:30px;
font-size:20px;
}
td{
width:150px;
height:50px;
text-align:center;}
#t1{
width:400px;
height:50px;
text-align:center;
}
</style>
</head>
<body>
<table border="2" align="center">
<tr >
<td colspan="3" rowspan="2"><input type="text" id="t1" ></td>
<td rowspan="2"><input type="button" id="c" value="C" οnclick="clear1()"/></td>
<td rowspan="2"><input type="button" id="c" value="CE" οnclick="del()"/></td>
</tr>
<tr>
</tr>
<tr>
<td >
<input type="button" id="7" value="7" οnclick="test(this.value)"/>
</td>
<td>
<input type="button" id="8" value="8" οnclick="test(this.value)" align="center"/>
</td>
<td>
<input type="button" id="9" value="9" οnclick="test(this.value)" align="center"/>
</td>
<td>
<input type="button" value="+/-" οnclick="comp()" align="center"/>
</td>
<td>
<input type="button" id="%" value="%" οnclick="op(this.value)" align="center"/>
</td>
</tr>
<tr>
<td>
<input type="button" id="4" value="4" οnclick="test(this.value)" align="center"/>
</td>
<td>
<input type="button" id="5" value="5" οnclick="test(this.value)" align="center"/>
</td>
<td>
<input type="button" id="6" value="6" οnclick="test(this.value)" align="center"/>
</td>
<td>
<input type="button" id="+" value="+" οnclick="op(this.value)" align="center"/>
</td>
<td>
<input type="button" id="-" value="-" οnclick="op(this.value)" align="center"/>
</td>
</tr>
<tr>
<td>
<input type="button" id="1" value="1" οnclick="test(this.value)" align="center"/>
</td>
<td>
<input type="button" id="2" value="2" οnclick="test(this.value)" align="center"/>
</td>
<td>
<input type="button" id="3" value="3" οnclick="test(this.value)" align="center"/>
</td>
<td>
<input type="button" id="*" value="*" οnclick="op(this.value)" align="center"/>
</td>
<td>
<input type="button" id="/" value="/" οnclick="op(this.value)" align="center"/>
</td>
</tr>
<tr>
<td>
<input type="button" id="0" value="0" οnclick="test(this.value)" align="center"/>
</td>
<td>
<input type="button" id="." value="." οnclick="test(this.value)" align="center"/>
</td>
<td></td>
<td><input type="button" id="=" value="=" οnclick="ev()"/></td>
<td><input type="button" value="返回"/></td>
</tr>
</table>
</body>
</html>