HTML5網(wǎng)頁計(jì)算器是一款基于最新的HTML5標(biāo)準(zhǔn)編寫的計(jì)算器,其源代碼可以幫助初學(xué)者更好地理解和學(xué)習(xí)HTML5。下面是該計(jì)算器的源代碼:
<html> <head> <title>HTML5計(jì)算器</title> <script> function calculate() { var num1 = parseFloat(document.getElementById("num1").value); var num2 = parseFloat(document.getElementById("num2").value); var operator = document.getElementById("operator").value; var result = 0; switch(operator) { case "+": result = num1 + num2; break; case "-": result = num1 - num2; break; case "*": result = num1 * num2; break; case "/": result = num1 / num2; break; } document.getElementById("result").innerHTML = "結(jié)果: " + result; } </script> </head> <body> <form> <label>數(shù)字1:</label><input type="number" id="num1"><br> <label>數(shù)字2:</label><input type="number" id="num2"><br> <label>操作符:</label><input type="text" id="operator"><br> <input type="button" value="計(jì)算" onclick="calculate()"><br><br> <div id="result"></div> </form> </body> </html>
該計(jì)算器通過獲取表單中的數(shù)字和操作符,使用switch語句計(jì)算結(jié)果,并將結(jié)果顯示在頁面上。代碼中的注釋和標(biāo)簽都非常清晰明了,方便理解和修改。