I searched the "Calculator" in JavaScript and found only three records. So I sent it out. Today it's okay to write and play. It is relatively simple. There is no technical content or practical value. It is only for beginners to learn. Experts can see if there is no problem and try again.
Test environment: IE6 + firefox1.5 passed the test
Technical description:
1. documnet. getelementsbytagname usage
2. bind an event to an html object: element. event name = function name
3. Eval usage
Source code:
<! Doctype HTML public "-// W3C // dtd html 4.01 // en" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> simple calculator-JavaScript version </title>
</Head>
<Style type = "text/CSS">
<! --
Body TD {
Font-size: 12px;
}
Table {
Border: 2px outset # f5f5f5;
Background-color: # d6d3ce;
}
Input {
Font-family: Arial, Helvetica, sans-serif;
Font-size: 12px;
Width: 24px;
Height: 24px;
Color: # 0000ff;
}
. R {
Width: 120px;
Height: auto;
Background-color: # f5f5f5;
Color: #000000;
}
. B {
Width: 80px;
Color: # ff0000;
}
. Color_red {
Color: # ff0000;
}
-->
</Style>
<Script language = "JavaScript" type = "text/JavaScript">
<! --
Window. onload = function (){
VaR elements = Document. getelementsbytagname ("input ");
For (I = 0; I <elements. length; I ++ ){
If (elements [I]. type = "button "){
Switch (elements [I]. ID ){
Case "B": elements [I]. onclick = backspace; break;
Case "C": elements [I]. onclick = clear; break;
Case "=": elements [I]. onclick = calculate; break;
Default: elements [I]. onclick = break;
}
}
}
}
Function backspace (){
VaR value = This. Form. R. value;
This. Form. R. value = value. substr (0, value. Length-1 );
}
Function clear (){
This. Form. R. value = "";
}
Function calculate (){
Try {
VaR result = eval (this. Form. R. value );
If (isnan (result )){
This. Form. R. value = "cannot be calculated ";
} Else if (! Isfinite (result )){
This. Form. R. value = "meaningless ";
} Else {
This. Form. R. value = result;
}
} Catch (e ){
This. Form. R. value = "error ";
}
}
Function compute (){
This. Form. R. Value + = This. value;
}
// -->
</SCRIPT>
<Body>
<Form ID = "form1" name = "form1" method = "Post" Action = "">
<Table width = "100" border = "0" align = "center" cellpadding = "3" cellspacing = "1">
<Tr>
<TD colspan = "4"> <input name = "R" type = "text" class = "R" id = "R" size = "16" maxlength = "16" readonly = "readonly"/> </TD>
</Tr>
<Tr>
<TD colspan = "3" align = "right"> <input name = "B" type = "button" class = "B" id = "B" value = "backspace" /> </TD>
<TD> <input name = "C" type = "button" class = "color_red" id = "C" value = "C"/> </TD>
</Tr>
<Tr>
<TD> & nbsp; </TD>
<TD> & nbsp; </TD>
<TD> <input name = "(" type = "button" class = "color_red" id = "(" value = "("/> </TD>
<TD> <input name = ")" type = "button" class = "color_red" id = ")" value = ")"/> </TD>
</Tr>
<Tr>
<TD> <input name = "7" type = "button" id = "7" value = "7"/> </TD>
<TD> <input name = "8" type = "button" id = "8" value = "8"/> </TD>
<TD> <input name = "9" type = "button" id = "9" value = "9"/> </TD>
<TD> <input name = "/" type = "button" class = "color_red" id = "/" value = "/"/> </TD>
</Tr>
<Tr>
<TD> <input name = "4" type = "button" id = "4" value = "4"/> </TD>
<TD> <input name = "5" type = "button" id = "5" value = "5"/> </TD>
<TD> <input name = "6" type = "button" id = "6" value = "6"/> </TD>
<TD> <input name = "*" type = "button" class = "color_red" id = "*" value = "*"/> </TD>
</Tr>
<Tr>
<TD> <input name = "1" type = "button" id = "1" value = "1"/> </TD>
<TD> <input name = "2" type = "button" id = "2" value = "2"/> </TD>
<TD> <input name = "3" type = "button" id = "3" value = "3"/> </TD>
<TD> <input name = "-" type = "button" class = "color_red" id = "-" value = "-"/> </TD>
</Tr>
<Tr>
<TD> <input name = "0" type = "button" id = "0" value = "0"/> </TD>
<TD> <input name = "." type = "button" id = "." value = "."/> </TD>
<TD> <input name = "=" type = "button" class = "color_red" id = "=" value = "="/> </TD>
<TD> <input name = "1 +" type = "button" class = "color_red" id = "1 +" value = "+"/> </TD>
</Tr>
</Table>
</Form>
</Body>
</Html>