O método deste artigo é super simples e vale muito a pena recomendar as idéias. Por favor, consulte isso.
A cópia do código é a seguinte:
<! Doctype html>
<html>
<head>
<meta http-equiv = "content-type" content = "text /html; charset = utf-8" />
<title> calculadora simples javascript </ititle>
<Cript>
// define a variável global var one = "1";
var dois = "2";
var três = "3";
var quatro = "4";
var cinco = "5";
var seis = "6";
var sete = "7";
var oito = "8";
var nine = "9";
var zero = "0";
var plus = "+", menos = "-", multiply = "*", divide = "/", decimal = ".";
função enter (obj, string) {
obj.expr.value+= string;
}
função compute (obj) {// clique na função igual ao botão "=" para chamar; obj.expr.Value = Eval (obj.expr.value); // Use JS para obter o método Eval () para calcular o nome como a fórmula em Expr}
</script>
</head>
<Body>
<nome de forma = "calc" id = "calc" action = "">
<tabela>
<td colspan = "4"> <input type = "text" name = "expr" size = "30" action = "compute (this.form)" /> </td>
<tr>
<Td> <input type = "button" value = "7" onclick = "enter (this.form, sete)" /> </td>
<Td> <input type = "button" value = "8" onclick = "enter (this.form, oito)" /> </td>
<td> <input type = "button" value = "9" onclick = "enter (this.form, nove)" /> </td>
<Td> <input type = "button" value = " /" onclick = "enter (this.form, divide)" /> </td>
</tr>
<tr>
<Td> <input type = "button" value = "4" onclick = "enter (this.form, quatro)" /> </td>
<Td> <input type = "button" value = "5" onclick = "enter (this.form, cinco)" /> </td>
<Td> <input type = "button" value = "6" onclick = "enter (this.form, seis)" /> </td>
<Td> <input type = "button" value = " *" onclick = "enter (this.form, multiply)" /> </td>
</tr>
<tr>
<Td> <input type = "button" value = "1" onclick = "enter (this.form, um)" /> </td>
<Td> <input type = "button" value = "2" onclick = "enter (this.form, dois)" /> </td>
<Td> <input type = "button" value = "3" onclick = "enter (this.form, três)" /> </td>
<Td> <input type = "button" value = " -" onclick = "enter (this.form, menos)" /> </td>
</tr>
<tr>
<td colspan = "2"> <input type = "button" value = "0" onclick = "enter (this.form, zero)" /> </td>
<Td> <input type = "button" value = "." OnClick = "Enter (this.form, decimal)" /> </td>
<td> <input type = "button" value = " +" onclick = "enter (this.form, plus)" /> </td>
</tr>
<tr>
<td colspan = "2"> <input type = "button" value = "=" onclick = "compute (this.form)" /> </td>
<td colspan = "2"> <input type = "button" value = "ac" onclick = "form.reset ()"/> </td>
</tr>
</tabela>
</morm>
</body>
</html>
É um código super NB, um nível mestre!