HTML5實現科學計數器源代碼示例:
<!DOCTYPE html> <html> <head> <title>科學計數器</title> <meta charset="utf-8"> </head> <body> <input id="display" type="text"/> <br/> <button onclick="del()">del</button> <button onclick="num('7')">7</button> <button onclick="num('8')">8</button> <button onclick="num('9')">9</button> <button onclick="op('+')">+</button> <br/> <button onclick="num('4')">4</button> <button onclick="num('5')">5</button> <button onclick="num('6')">6</button> <button onclick="op('-')">-</button> <br/> <button onclick="num('1')">1</button> <button onclick="num('2')">2</button> <button onclick="num('3')">3</button> <button onclick="op('*')">*</button> <br/> <button onclick="clearScreen()">clear</button> <button onclick="num('0')">0</button> <button onclick="dot()">.</button> <button onclick="op('/')">/</button> <br/> <button onclick="equal()">=</button> </body> <script> var num1 = 0; var num2 = 0; var operator = ''; var result = 0; function num(num) { document.getElementById("display").value += num; } function op(op) { num1 = parseFloat(document.getElementById("display").value); operator = op; document.getElementById("display").value = ''; } function equal() { num2 = parseFloat(document.getElementById("display").value); switch (operator) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; break; } document.getElementById("display").value = result.toString(); } function dot() { if (document.getElementById("display").value.indexOf(".") == -1) { document.getElementById("display").value += '.'; } } function del() { var str = document.getElementById("display").value; document.getElementById("display").value = str.substring(0, str.length - 1); } function clearScreen() { document.getElementById("display").value = ''; } </script> </html>