怎样用JavaScript编译简易计算器??
getElementsByName这个获得的是字符串记得用parseInt转换成int哦
基本的+ – x /
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<!DOCTYPE html> <html> <head> <title> 事件</title> <script type="text/javascript"> function count1(){ var a=parseInt(document.getElementById('txt1').value); var b=parseInt(document.getElementById('txt2').value); var add4=document.getElementById('select').value; var c=0; switch(add4){ case '+': c= a+b; break; case '-': c= a-b; break; case '*': c= a*b; break; case '/': c= a/b; } document.getElementById('fruit').value=c; } </script> </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='count1()'/> <!--通过 = 按钮来调用创建的函数,得到结果--> <input type='text' id='fruit' /> </body> </html> |