今天看书无意间发现有这么好用的方法,只要传一个算术表达式进去就能得到最终值。这样开发一个计算器变得非常容易了,而且小数点都支持…
下面是我写的代码。高手就别喷啦,我才开始学JavaScript
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>JavaScript计算器</title> <script> </script> </head> <body> <center><h2>JavaScript计算器</h2> <table width="293" border="0"> <tr style="text-align:center;"> <td width="241"><input type="text" width="200px" id="textbox"/><input type="button" value="求值" onclick="document.getElementById('textbox').value =eval(document.getElementById('textbox').value)"></td> </tr> </table> <br /> <table width="200" border="0"> <tr> <td><input type="button" value="7" onclick="document.getElementById('textbox').value+='7'"/></td> <td><input type="button" value="8" onclick="document.getElementById('textbox').value+='8'"/></td> <td><input type="button" value="9" onclick="document.getElementById('textbox').value+='9'"/></td> <td><input type="button" value="加" onclick="document.getElementById('textbox').value+='+'"/></td> </tr> <tr> <td><input type="button" value="4" onclick="document.getElementById('textbox').value+='4'"/></td> <td><input type="button" value="5" onclick="document.getElementById('textbox').value+='5'"/></td> <td><input type="button" value="6" onclick="document.getElementById('textbox').value+='6'"/></td> <td><input type="button" value="减" onclick="document.getElementById('textbox').value+='-'"/></td> </tr> <tr> <td><input type="button" value="1" onclick="document.getElementById('textbox').value+='1'"/></td> <td><input type="button" value="2" onclick="document.getElementById('textbox').value+='2'"/></td> <td><input type="button" value="3" onclick="document.getElementById('textbox').value+='3'"/></td> <td><input type="button" value="乘" onclick="document.getElementById('textbox').value+='*'"/></td> </tr> <tr> <td><input type="button" value="." onclick="document.getElementById('textbox').value+='.'"/></td> <td><input type="button" value="0" onclick="document.getElementById('textbox').value+='0'" /></td> <td><input type="button" value="R" onclick="document.getElementById('textbox').value=''"/></td> <td><input type="button" value="除" onclick="document.getElementById('textbox').value+='/'"/></td> </tr> </table> <p> </p> <p>Powerd By Zmsky</p> </center> </body> </html>
JavaScript计算器
Powerd By Zmsky
很好 ,很强,支持博主。
这是一个测试,akismet测试
哈哈哈,不错,原产品,第一手啊。刚刚测试,可以~!
JS看不懂~~~~ %xyq42
喂喂,还是同行呢!!
还可以做各种各样邪恶的事情啦~.~/