代碼以下:html
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>js計算器</title> <script> function calculator(){ var num1 = parseFloat(document.getElementById("num1").value); var num2 = parseFloat(document.getElementById("num2").value); var result = parseFloat(document.getElementById("result").value); var mark = document.getElementById("mark").value; switch (mark){ case "+" : result = num1 + num2; break; case "-" : result = num1 - num2; break; case "*" : result = num1 * num2; break; case "/" : result = num1 / num2; break; } document.getElementById("result").value = result; } </script> </head> <body> <input type="number" id="num1" > <select id="mark"> <option> + </option> <option> - </option> <option> * </option> <option> / </option> </select> <input type="number" id="num2" > <button onclick="calculat0r()"> = </button> <input type="text" id="result" > </body> </html>
效果以下:spa