您的位置:首页 > 其它

计算器简单实现

2013-02-27 20:31 148 查看
<html>

<head>

<script>

var flag = true;

function getNum(num){

if(!flag){

document.getElementById('res').value = '';

flag = true;

}

document.getElementById('res').value += num;

}

function getResult(){

var result = eval(document.getElementById('res').value);

document.getElementById('res').value = result;

flag = false;

}

</script>

</head>

<body>

<table border='1px' width='300px'>

<caption>计算器</caption>

<tr>

<td colspan='4'><input type='text' id='res' size=47></td>

</tr>

<tr>

<td><input type='button' value='1' style="width:75px" onclick="getNum(1)"></td>

<td><input type='button' value='2' style="width:75px" onclick="getNum(2)"></td>

<td><input type='button' value='3' style="width:75px" onclick="getNum(3)"></td>

<td><input type='button' value='+' style="width:75px" onclick="getNum('+')"></td>

</tr>

<tr>

<td><input type='button' value='4' style="width:75px" onclick="getNum(4)"></td>

<td><input type='button' value='5' style="width:75px" onclick="getNum(5)"></td>

<td><input type='button' value='6' style="width:75px" onclick="getNum(6)"></td>

<td><input type='button' value='-' style="width:75px" onclick="getNum('-')"></td>

</tr>

<tr>

<td><input type='button' value='7' style="width:75px" onclick="getNum(7)"></td>

<td><input type='button' value='8' style="width:75px" onclick="getNum(8)"></td>

<td><input type='button' value='9' style="width:75px" onclick="getNum(9)"></td>

<td><input type='button' value='*' style="width:75px" onclick="getNum('*')"></td>

</tr>

<tr>

<td><input type='button' value='0' style="width:75px" onclick="getNum(0)"></td>

<td><input type='button' value='.' style="width:75px" onclick="getNum('.')"></td>

<td><input type='button' value='=' style="width:75px" onclick="getResult()"></td>

<td><input type='button' value='/' style="width:75px" onclick="getNum('/')"></td>

</tr>

</table>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: