Hoje eu estava entediado e queria escrever algo, mas de repente pensei em escrever uma calculadora em JavaScript. Ainda existem muitos bugs no programa. Vou gravá -lo aqui primeiro e depois corrigi -lo lentamente mais tarde.
O código é o seguinte:
<! xmlns = "http://www.w3.org/1999/xhtml"> <head> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"/> title> Javascript Código para implementar o calculculculculculvador simples <wulin.com 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; resultado; // salve o valor numérico na caixa de entrada antes do operador de clique; // Salvar o operador var isPresseCalsKey = false; // Registre se deve pressionar ou não "=" key // Função do evento Número da função ConnectionDigital (Control) {var txt = document.getElementById ('txtScream'); if (isPressequalsKey) {txt.value = ""; // O cálculo foi realizado, depois limpe a caixa de entrada numérica e inicie novamente o isPressECalsKey = false;} // O ponto decimal já existe na entrada numérica, o ponto decimal não pode ser inserido se (TXT.Value.Indexof ('.')> -1 && Control.value == '. // atribui o valor de controle à caixa de entrada numérica} // Função de eventos backspace backspace () {var txt = document.getElementById ('txtScream'); txt.value = txt.value.substring (0, txt.value.length - 1);} // CECE Evento: clearall () {document.getElementById ('txtScream'). value = ""; resultado = ""; operator = "";} // +, -, *,/cálculo da função de evento (controle) {// salvar o operador no operador variável global = control.value; var txt = document.getElementById ('txtScream'); if (txt.value == "") retornar false; // Não há número na caixa de entrada numérica, o operador não pode ser inserido // salve o valor na caixa de entrada numérica no resultado da expressão de cálculo = txt.value; // Limpe a caixa de entrada a ser inserida no valor da operação txt.value = ""; } // a função de resultado do cálculo getResult () {var opValue; // O operador var fonte de valor = parsefloat (resultado); var txt = document.getElementById ('txtScream'); if (operator == ' *) OPValue = SourseValue * parsefloat (txt.value); parsefloat (txt.Value); else if (operador == ' +') opValue = SourseValue + parsefloat (txt.value); else if (operator == ' -') opvalue = soursevalue - parsefloat (txt.value); "";} </script> </ad Head> <body> <tabela id = "tbcalculator" align = "central" CellPadding = "0" CellPacing = "0" borderColor = "#0066ff"> <tr> <td colspan = "4ign =" center "> <input type =" text = "names =" = ") readOnly = "readonly"/> </td> </tr> <tr> <td colspan = "2"> <input type = "button" name = "btnce" id = "btnce" value = "ce" align = "direita"; OnClick = "Clearall ();" /> </td> <td colspan = "2"> <input type = "button" name = "btn10" id = "btn10" value = "backspace" align = "direita"; 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> <tv) <input type = "button" "" btn9 "id =" btn9 "; /> </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 = "conexão de conexão (this);"/> </td> <td> <input type = "" name = "btn5" id = "bt> bt>" OnClick = "ConnectionDigital (this);"/> </td> <td> <input type = "button" name = "btn6" id = "btn6" value = "6" onclick = "conexionDigital (the); OnClick = "Cálculo (this);" /> </td> </tr> <td> <td> <input type = "button" name = "btn1" id = "btn1" value = "1" onclick = "conexão de conexão (this);"/> </td> <td> <input type = "" name "=" btn2 "ids =" bnt> bn2 "ida" 2 "isto"/"r" = "/td> <td> <input type =" "name" = "btn2" ids = "bnt> bn2" ida "2"/"r" = "/td> <td> <input Type =" "name =" btn2 "id =" Bnt "Bnt" 2 "ida"/"r" = "/td> 2" ida "/" ida "/" ida "2" isto); OnClick = "ConnectionDigital (this);"/> </td> <td> <input type = "button" name = "btn3" id = "btn3" value = "3" onclick = "conexionDigital (the); OnClick = "Cálculo (this);" /> </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 (this);" /> </td> <td> <nome de entrada = "btn22" type = "button" id = "btn22" value = "=" onclick = "getResult ();" /> </td> <td> <input type = "button" name = "btn23" id = "btn23" value = "+" onclick = "cálculo (this);" /> </td> </tr> </tabela> </body> </html>O código acima para implementar a calculadora simples no JavaScript é todo o conteúdo que compartilhei com você. Espero que você possa lhe dar uma referência e espero que você possa apoiar mais o wulin.com.