Posted in Javascript onNovember 18, 2020
本文实例为大家分享了js实现简易计算器的具体代码,供大家参考,具体内容如下
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> ul li{ list-style: none; line-height: 50px; } .getAnswer{ width: 20px; } </style> </head> <body> <form name="calc"> <h2>简易计算器</h2> <ul> <li> 第一个数: <input type="text" name="num1" ></li> <li> 第二个数: <input type="text" name="num2"></li> <li> <input name="getAnswer" type="button" onclick="calcu1()" value="+"> <input name="getAnswer" type="button" onclick="calcu2()" value="-"> <input name="getAnswer" type="button" onclick="calcu3()" value="*"> <input name="getAnswer" type="button" onclick="calcu4()" value="/"> </li> <li>计算结果: <input type="text" name="result" id="result"> </li> </ul> </form> <script type="text/javascript"> function calcu1() { var num1=document.calc.num1.value; var num2=document.calc.num2.value; sum1=parseFloat(num1) + parseFloat(num2); var total = num1+"+"+num2+"="+sum1; document.calc.result.value=total; } function calcu2() { var num1=document.calc.num1.value; var num2=document.calc.num2.value; sum1=parseFloat(num1) - parseFloat(num2); var total = num1+"-"+num2+"="+sum1; document.calc.result.value=total; } function calcu3() { var num1=document.calc.num1.value; var num2=document.calc.num2.value; var total = num1+"*"+num2+"="+parseFloat(num1)*parseFloat(num2); document.calc.result.value=total; } function calcu4() { var num1=document.calc.num1.value; var num2=document.calc.num2.value; if (num2==0){ alert("除数不能为0,请重新输入"); }else{ var total = num1+"/"+num2+"="+parseFloat(num1) / parseFloat(num2); document.calc.result.value=total; } } </script> </body> </html>
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。
js实现简易计算器小功能
- Author -
Ms_Joanna声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@