Hoy estaba aburrido y quería escribir algo, pero de repente pensé en escribir una calculadora en JavaScript. Todavía hay muchos errores en el programa. Lo grabaré aquí primero y luego lo corrigiré lentamente más tarde.
El código es el siguiente:
< xmlns = "http://www.w3.org/1999/xhtml"> <foad> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"/> <title> JavaScript Code para implementar calculador simple_wulin.com </title> type = "text/css"> input {width: 30px; altura: 20px; text-align: center;}#tbcalculator td {text-align: center; vertical-align: middle;} </style> <script type = "text/javascript"> var dulte; // Guardar el valor numérico en el cuadro de entrada antes del operador de clic; // Guardar el operador var ispressequalskey = false; // Registre si debe presionar o no "=" Key // Número Key Función ConnectionDigital (Control) {var txt = document.getElementById ('txtScream'); if (isPressequalSkey) {txt.value = ""; // El cálculo se ha realizado, luego borre el cuadro de entrada numérico e inicie nuevamente iSpressequalskey = false;} // El punto decimal ya existe en la entrada numérica, entonces el punto decimal no se permite que se ingrese si (txt.value.indexof ('.')> -1 && control.value == '. // Asigna el valor de control al cuadro de entrada numérico} // Backspace Event Function Backspace () {var txt = document.getElementById ('txtscream'); txt.value = txt.value.substring (0, txt.value.length - 1);} // ce key event: borrar la función numérica de entrada clearAll () {document.getElementById ('txtScream'). value = ""; result = ""; operator = "";} // +, -, *,/event Function Cálculo (control) {// Guardar el operador en el operador variable global = control.value; var txt = document.getElementById ('txtscream'); if (txt.value == "") return false; // No hay número en el cuadro de entrada numérica, entonces el operador no se puede ingresar // Guardar el valor en el cuadro de entrada numérica en la expresión de cálculo resultado = txt.value; // borrar el cuadro de entrada que se ingresará al valor de operación txt.value = ""; } // La función de resultado de cálculo getResult () {var opValue; // el operador var fourceValue = parsefloat (resultado); var txt = document.getElementById ('txtScream'); if (operator == ' *') opvalue = sourseValue * parsefloat (txt.value); más if (operator == /=/'/' SourseValue / parsefloat (txt.Value); else if (operator == ' +') opValue = SourseValue + parsefloat (txt.value); else if (operator == ' -') opvalue = sourseValue - ""; opvalue = "";} </script> </head> <body> <table id = "tbcalculator" align = "center" cellPadding = "0" celular = "0" borderColor = "#0066ff"> <tr> <td colspan = "4" align = "center"> <input type = "text" name = "name =" txtscream "id" id = "id =" txtscheam "Id. readonly = "readonly"/> </td> </tr> <tr> <td colspan = "2"> <input type = "button" name = "btnce" id = "btnce" value = "ce" align = "right"; onClick = "clearAll ();" /> </td> <td colspan = "2"> <input type = "button" name = "btn10" id = "btn10" valor = "backspace" align = "right"; onClick = "Backspace ();" /> </td> </tr> <tt> <td> <input type = "button" name = "btn7" id = "btn7" value = "7" onClick = "ConnectionDigital (this);" /> </td> <td> <input type = "button" name = "btn8" id = "btn8" value = "8" onClick = "ConnectionDigital (this);"/> </td> <td> <input type = "button" name = "btn9" id = "btn9" value = "9" oncclick = "ConnectionDigital (this);"; " /> </td> <td> <input type = "button" name = "btn6" id = "btn6" value = "/" onClick = "Cálculo (this);" /></td></tr><tt><td><input type="button" name="btn4" id="btn4" value="4" onclick="connectionDigital(this);"/></td><td><input type="button" name="btn5" id="btn5" value="5" onClick = "ConnectionDigital (this);"/> </td> <td> <input type = "button" name = "btn6" id = "btn6" valor = "6" onClick = "ConnectionDigital (this);"/> </td> <td> <input type = "name =" btn13 "id =" btn13 "valor =" "*"*""*"*"*"*"*"*"*"*"*" onClick = "Cálculo (esto);" /> </td> </tr> <td> <td> <input type = "button" name = "btn1" id = "btn1" value = "1" onClick = "ConnectionDigital (this);"/> </td> <td> <input type = "button" name = "btn2" id = "btn2" id = "btn2" value "value" 2 "2" 2 "2" onClick = "ConnectionDigital (this);"/> </td> <td> <input type = "button" name = "btn3" id = "btn3" value = "3" onClick = "ConnectionDigital (this);"/> </td> <td> <input type = "name =" btn18 "id =" btn18 "valor ="-"-"-""-"" onClick = "Cálculo (esto);" /> </td> </tr> <tr> <td> <input type = "button" name = "btn0" id = "btn0" value = "0" onClick = "ConnectionDigital (this);" /> </td> <td> <input type = "button" name = "btndot" id = "btndot" value = "." onClick = "ConnectionDigital (esto);" /> </td> <td> <input name = "btn22" type = "botón" id = "btn22" value = "=" onClick = "getResult ();" /> </td> <td> <input type = "Button" name = "btn23" id = "btn23" value = "+" onClick = "Cálculo (this);" /> </td> </tr> </table> </body> </html>El código anterior para implementar una calculadora simple en JavaScript es todo el contenido que he compartido con usted. Espero que pueda darle una referencia y espero que pueda apoyar más a Wulin.com.