<!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>
<title></title>
<script type="text/javascript">
window.onload = function () {
var result = document.getElementById("txtResult"); //获取值得ID
var denghao = document.getElementById("txtDenghao"); //获取等号的ID
var qingchu = document.getElementById("txtClear");
//给name是shuzi的加上onclick事件
var btnshuziArr = document.getElementsByName("shuzi");
for (var i = 0; i < btnshuziArr.length; i++) {
btnshuziArr[i].onclick = function () {
result.value += this.value;//this代表你点击时的那个
}
}
//结果
denghao.onclick = function () {
result.value = eval(result.value);
}
//清除
qingchu.onclick = function () {
result.value = "";
}
}
</script>
</head>
<body>
<table cellpadding="0" cellspacing="0">
<tr>
<td colspan="4">
<input id="txtResult" name="result" type="text" value="" />
</td>
</tr>
<tr>
<td colspan="2">
<input id="txtDenghao" name="denghao" type="button" value="=" />
</td>
<td colspan="2">
<input id="txtClear" name="clear" value="清除" type="button" />
</td>
</tr>
<tr>
<td>
<input name="shuzi" value="7" type="button" />
</td>
<td>
<input name="shuzi" value="8" type="button" />
</td>
<td>
<input name="shuzi" value="9" type="button" />
</td>
<td>
<input name="shuzi" value="/" type="button" />
</td>
</tr>
<tr>
<td>
<input name="shuzi" value="4" type="button" />
</td>
<td>
<input name="shuzi" value="5" type="button" />
</td>
<td>
<input name="shuzi" value="6" type="button" />
</td>
<td>
<input name="shuzi" value="*" type="button" />
</td>
</tr>
<tr>
<td>
<input name="shuzi" value="1" type="button" />
</td>
<td>
<input name="shuzi" value="2" type="button" />
</td>
<td>
<input name="shuzi" value="3" type="button" />
</td>
<td>
<input name="shuzi" value="-" type="button" />
</td>
</tr>
<tr>
<td colspan="2">
<input name="shuzi" value="0" type="button" />
</td>
<td colspan="2">
<input name="shuzi" value="+" type="button" />
</td>
</tr>
</table>
</body>
</html>
js简单的计算器(使用eval)
最新推荐文章于 2024-04-05 06:47:12 发布