JavaScript实例之计算器
代码实例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>计算器实例效果</title> <script type="text/javascript"> var compute=function (val) { var num1=parseFloat(document.getElementById("num1").value); var num2=parseFloat(document.getElementById("num2").value); var num3=document.getElementById("num3"); if(val=='+') { num3.value=num1+num2; } if(val=='-') { num3.value=num1-num2; } if(val=='*') { num3.value=num1*num2; } if(val=='/') { num3.value=num1/num2; } } </script> </head> <body> <h2>计算器</h2> 数字1:<input type="text" id="num1"><br/> 数字2:<input type="text" id="num2"><br/> <input type="button" value="+" onclick="compute(this.value)"/> <input type="button" value="-" onclick="compute(this.value)"/> <input type="button" value="*" onclick="compute(this.value)"/> <input type="button" value="/" onclick="compute(this.value)"/><br> 结果:<input type="text" id="num3"/><br> </body> </html>
效果:
2017-09-05 00:00:33
越努力越幸运