一个js实现的网页计算器雏形,看这个标题您就明白了,与我们以前说的计算器有很大的差距,这里只是在input文本框中进行了简单的运算,例如输入:1+2,按下计算,则清除原内容并显示:3,是不是有点太简单了呢?菜鸟朋友多参考一下,还是非常有帮助的。 示例: <html> <head> <meta http-equiv="content-type" content="text/html; charset=GB2312" /> <title>JavaScript计算器</title> <script> function calc(){ try{ document.getElementById("txt_formula").value = eval(document.getElementById("txt_formula").value); }catch(e){ document.getElementById("txt_formula").value = "错误的算式"; } } </script> </head> <body style="overflow:auto;"> <label>输入要计算的算式:</label> <input id="txt_formula"> <input type="button" value="计算" onclick="calc();"> </body> </html><br /><center>如不能显示效果,请按Ctrl+F5刷新本页,更多网页代码:<a href='http://www.veryhuo.com/' target='_blank'>http://www.veryhuo.com/</a></center>提示:可修改后代码再运行! 文章导航 JavaScript实现随机抽取预先设定的数值Unescape简单的Js加密与解密代码