Home >Web Front-end >JS Tutorial >Simple js calculator implementation
The example in this article shares the js calculator implementation code for your reference. The specific content is as follows
<!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>