简单的js计算器实现
程序员文章站
2022-03-30 16:01:14
...
本文实例为大家分享了js计算器实现代码,供大家参考,具体内容如下
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body > <input type='text' id='txt1' /> <select id='select'> <option value='+'>+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> <input type='text' id='txt2' /> <input type='button' value=' = ' onclick="count()"/> <!--通过 = 按钮来调用创建的函数,得到结果--> <input type='text' id='fruit' /> </body> <script type="text/javascript"> //制做简单计算器 function count() { var txt1 = document.all.txt1.value; var txt2 = document.all.txt2.value; var s=document.all.select.value; var z; switch(s){ case "+": z=parseFloat(txt1)+parseFloat(txt2);break; case "-": z=parseFloat(txt1)-parseFloat(txt2);break; case "*":z=parseFloat(txt1)*parseFloat(txt2);break; case "/":z=parseFloat(txt1)/parseFloat(txt2);break; } document.getElementById("fruit").value=z; } </script> </html>