javascript(一)计算器代码生成
在body区:
<table align="center" >
<tr>
<td colspan="4">
<input id="Text1" type="text" value="" style=" text-align:right;" /></td>
</tr>
<tr>
<td>
<input id="Button1" type="button" value="1" style="width:37px; height: 29px;"/></td>
<td><input id="Button2" type="button" value="2" style="width:37px; height: 29px;"/></td>
<td><input id="Button3" type="button" value="3" style="width:37px; height: 29px;"/></td>
<td><input id="Button4" type="button" value="+" style="width:37px; height: 29px;"/></td>
</tr>
<tr>
<td>
<input id="Button5" type="button" value="4" style="width:37px; height: 29px;"/></td>
<td><input id="Button6" type="button" value="5" style="width:37px; height: 29px;"/></td>
<td><input id="Button7" type="button" value="6" style="width:37px; height: 29px;"/></td>
<td><input id="Button8" type="button" value="-" style="width:37px; height: 29px;"/></td>
</tr>
<tr>
<td>
<input id="Button9" type="button" value="7" style="width:37px; height: 29px;"/></td>
<td><input id="Button10" type="button" value="8" style="width:37px; height: 29px;"/></td>
<td><input id="Button11" type="button" value="9" style="width:37px; height: 29px;"/></td>
<td><input id="Button12" type="button" value="*" style="width:37px; height: 29px;"/></td>
</tr>
<tr>
<td>
<input id="Button13" type="button" value="0" style="width:37px; height: 29px;"/></td>
<td><input id="Button14" type="button" value="." style="width:37px; height: 29px;"/></td>
<td><input id="Button15" type="button" value="ESC" style="width:37px; height: 29px;"/></td>
<td><input id="Button16" type="button" value="/" style="width:37px; height: 29px;"/></td>
</tr>
<tr>
<td colspan="4">
<input id="Button17" type="button" value="=" style="width:100%; height: 29px;"/>
</tr>
</table>
脚本区
<script type="text/javascript">
function dd() {
var v = document.getElementsByTagName("input");
for (var i = 0; i < v.length; i++) {
dd = v[i];
dd.onclick = initEvent;
}
}
var ss;
var a = 0;
function initEvent() {
var v = document.getElementsByTagName("input");
var s = indexOf(v, this);
var txt = document.getElementById("Text1");
switch (s) {
case 0: txt.value = ""; break;
case 1: txt.value = txt.value + "1"; break;
case 2: txt.value = txt.value + "2"; break;
case 3: txt.value = txt.value + "3"; break;
case 4: ss = "+"; a = txt.value; txt.value = ""; break;
case 5: txt.value = txt.value +"4"; break;
case 6: txt.value = txt.value + "5"; break;
case 7: txt.value = txt.value + "6"; break;
case 8: ss = "-"; a = txt.value; txt.value = ""; break;
case 9: txt.value = txt.value + "7"; break;
case 10: txt.value = txt.value + "8"; break;
case 11: txt.value = txt.value + "9"; break;
case 12: ss = "*"; a = txt.value; txt.value = ""; break;
case 13: txt.value = txt.value + "0"; break;
case 14: txt.value = txt.value + "."; ss = "."; break;
case 15: txt.value =""; break;
case 16: ss = "/"; a = txt.value; txt.value = ""; break;
case 17: switch (ss) {
case "+": txt.value = parseFloat(a) + parseFloat(txt.value); break;
case "-": txt.value = parseFloat(a) - parseFloat(txt.value); break;
case "*": txt.value = parseFloat(a) * parseFloat(txt.value); break;
case "/": if (parseFloat(a)> parseFloat(txt.value)) { txt.value = parseFloat(a) / parseFloat(txt.value); ; break; } else { alert("被除数小于除数!不能相除!") }
}
}
}
function indexOf(arr, element) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] == element) {
return i;
return -1;
}
}
}
</script>
本文来自博客园,作者:风痕天下,转载请注明原文链接:https://www.cnblogs.com/xuxiaorong/archive/2012/03/27/2420364.html