Metode artikel ini sangat sederhana, dan idenya sangat layak direkomendasikan. Silakan merujuknya.
Salinan kode adalah sebagai berikut:
<! Doctype html>
<Html>
<head>
<meta http-equiv = "konten tipe" content = "text /html; charset = utf-8" />
<title> JavaScript Simple Calculator </iteme>
<script>
// Tentukan variabel global var satu = "1";
var dua = "2";
var tiga = "3";
var empat = "4";
var lima = "5";
var enam = "6";
var tujuh = "7";
var delapan = "8";
var sembilan = "9";
var zero = "0";
var plus = "+", minus = "-", multiply = "*", divide = "/", decimal = ".";
fungsi enter (obj, string) {
obj.expr.value+= string;
}
function compute (obj) {// klik fungsi yang sama dengan tombol "=" untuk menelepon; obj.expr.value = eval (obj.expr.value); // gunakan js untuk mendapatkan metode eval () untuk menghitung nama sebagai rumus dalam expr}
</script>
</head>
<body>
<Form name = "calc" id = "calc" action = "">
<able>
<td colspan = "4"> <input type = "text" name = "expr" size = "30" action = "compute (this.form)" /> </td>
<tr>
<td> <input type = "tombol" value = "7" onclick = "enter (this.form, tujuh)" /> </td>
<td> <input type = "tombol" value = "8" onclick = "enter (this.form, delapan)" /> </td>
<td> <input type = "tombol" value = "9" onclick = "enter (this.form, sembilan)" /> </td>
<td> <input type = "tombol" value = " /" onclick = "enter (this.form, divide)" /> </td>
</tr>
<tr>
<td> <input type = "tombol" value = "4" onclick = "enter (this.form, four)" /> </td>
<td> <input type = "tombol" value = "5" onclick = "enter (this.form, lima)" /> </td>
<td> <input type = "tombol" value = "6" onclick = "enter (this.form, enam)" /> </td>
<td> <input type = "tombol" value = " *" onclick = "enter (this.form, multiply)" /> </td>
</tr>
<tr>
<td> <input type = "tombol" value = "1" onclick = "enter (this.form, one)" /> </td>
<td> <input type = "tombol" value = "2" onclick = "enter (this.form, two)" /> </td>
<td> <input type = "tombol" value = "3" onclick = "enter (this.form, tiga)" /> </td>
<td> <input type = "tombol" value = " -" onclick = "enter (this.form, minus)" /> </td>
</tr>
<tr>
<td colspan = "2"> <input type = "tombol" value = "0" onclick = "enter (this.form, nol)" /> </td>
<td> <input type = "tombol" value = "." onclick = "enter (this.form, desimal)" /> </td>
<td> <input type = "tombol" value = " +" onclick = "enter (this.form, plus)" /> </td>
</tr>
<tr>
<td colspan = "2"> <input type = "tombol" value = "=" onclick = "compute (this.form)" /> </td>
<td colspan = "2"> <input type = "tombol" value = "ac" onclick = "form.reset ()"/> </td>
</tr>
</boable>
</form>
</body>
</html>
Apakah ini kode NB super, level master!