资料
表格
制度
合同
管理
职场
经营
创业
范文
总结
计划
作文
文章
美文
词句
教程
模板
图片
字体
图标
办公
人事
财务
生产
管理资源吧首页
>>>
教程
>>>
编程
>>>
JavaScript教程
>>>
用JS写的简单的计算器实现代码
1.本页效果图片
2.美化后的效果
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <img src="/_New/_cj/myeditor/sysimage/space.gif" class="ewebeditor__script" _ewebeditor_fake_tag="script" _ewebeditor_fake_value="%3Cscript%3E%20%0D%0AArray.prototype.remove%3Dfunction(index)%20%0D%0A%7B%20%0D%0Aif(isNaN(index)%7C%7Cindex%3Ethis.length)%7Breturn%20false%3B%7D%20%0D%0Afor(var%20i%3D0%2Cn%3D0%3Bi%3Cthis.length%3Bi%2B%2B)%20%0D%0A%7B%20%0D%0Aif(this%5Bi%5D!%3Dthis%5Bindex%5D)%20%0D%0A%7B%20%0D%0Athis%5Bn%2B%2B%5D%3Dthis%5Bi%5D%20%0D%0A%7D%20%0D%0A%7D%20%0D%0Athis.length-%3D1%20%0D%0A%7D%20%0D%0A%0D%0Afunction%20commonMath()%7B%20%0D%0A%2F%2F%E7%B1%BB%E5%88%9D%E5%A7%8B%E5%8C%96%20%0D%0Athis.init%20%3D%20function()%7B%20%0D%0Athis.tmp%20%3D%20''%3B%20%0D%0Athis.debug%20%3D%200%3B%20%0D%0Athis.output%20%3D%20''%3B%20%0D%0Athis.method%20%3D%20''%3B%20%0D%0Athis.sign%20%3D%201%3B%2F%2F0%3A-%3B1%3A%2B%20%0D%0Athis.register%20%3D%20Array()%3B%2F%2F%E5%AF%84%E5%AD%98%E5%99%A8%20%0D%0A%7D%20%0D%0A%2F%2F%E8%AE%BE%E7%BD%AE%E6%8A%A5%E9%94%99%E4%BF%A1%E6%81%AF%20%0D%0Athis.showmsg%20%3D%20function(msg)%7B%20%0D%0Aif(this.debug%20%3D%3D%201)%20alert(msg)%3B%20%0D%0A%7D%20%0D%0A%2F%2F%E8%AE%BE%E7%BD%AE%E8%BF%90%E7%AE%97%E7%AC%A6%20%0D%0Athis.setMethod%20%3D%20function(_method)%7B%20%0D%0Athis.method%20%3D%20_method%3B%20%0D%0A_length%20%3D%20this.register.length%3B%20%0D%0Aif(_length%20%3D%3D%200)%7B%20%0D%0Aif(this.tmp%20%3D%3D%20'')%20return%3B%20%0D%0Athis.register%5B0%5D%20%3D%20this.tmp%3B%20%0D%0Athis.register%5B1%5D%20%3D%20_method%3B%20%0D%0Athis.tmp%20%3D%20''%3B%20%0D%0Areturn%3B%20%0D%0A%7D%20%0D%0Aif(_length%20%3D%202%20%7C%7C%20this.tmp%20%3D%3D%20'')%7B%20%0D%0Athis.register%5B1%5D%20%3D%20_method%3B%20%0D%0A%7D%20%0D%0Aif(_length%20%3D%3D%202%20%26%26%20this.tmp%20!%3D%20'')%7B%20%0D%0Athis.register%5B2%5D%20%3D%20this.tmp%3B%20%0D%0Athis.run(1)%3B%20%0D%0A%7D%20%0D%0Aif(_length%20%3D%3D%203)%20this.run(1)%3B%20%0D%0A%7D%20%0D%0A%2F%2F%E8%AE%BE%E7%BD%AE%E6%98%BE%E7%A4%BA%E5%80%BC%20%0D%0Athis.setValue%20%3D%20function(_value)%7B%20%0D%0Avar%20tmp%20%3D%20parseInt(eval(this.tmp%2B'%2B%22'%2B_value%2B'%22'))%3B%20%0D%0Amax%20%3D%20%2F%5E-%3F%5Cd%7B1%2C9%7D%24%2Fi%3B%20%0D%0Aif(%20max.test(tmp)%20%3D%3D%20false)%7B%20%0D%0Areturn%3B%20%0D%0A%7D%20%0D%0Athis.tmp%20%3D%20tmp%3B%20%0D%0Athis.output.value%20%3D%20this.tmp%3B%20%0D%0A%7D%20%0D%0A%2F%2F%E8%AE%BE%E7%BD%AE%E7%AC%A6%E5%8F%B7%20%0D%0Athis.setSign%20%3D%20function()%7B%20%0D%0Avar%20del%20%3D%200%3B%20%0D%0Avar%20sign%20%3D%20Array('-'%2C'%2B')%3B%20%0D%0Athis.sign%20%3D%20this.sign%20%5E%201%3B%20%0D%0A_sign%20%3D%20sign%5Bthis.sign%5D%3B%20%0D%0Aif(%2F%5Cd%2Fi.test(this.tmp)%3D%3D%20false%20%26%26%20this.register.length%20%3E%200)%7B%20%0D%0Adel%20%3D%201%3B%20%0D%0Athis.tmp%20%3D%20this.register%5B0%5D%3B%20%0D%0A%7D%20%0D%0Aif(_sign%20%3D%3D%20'-')%7B%20%0D%0Athis.tmp%20%3D%20-this.tmp%3B%20%0D%0A%7Delse%7B%20%0D%0Athis.tmp%20%3D%20Math.abs(this.tmp)%3B%20%0D%0A%7D%20%0D%0Athis.output.value%20%3D%20this.tmp%3B%20%0D%0Aif(del%20%3D%3D%201)%7B%20%0D%0Athis.register%5B0%5D%20%3D%20this.tmp%3B%20%0D%0Athis.tmp%20%3D%20''%3B%20%0D%0A%7D%20%0D%0A%7D%20%0D%0A%2F%2F%E8%8E%B7%E5%8F%96%E6%8C%89%E9%94%AE%20%0D%0Athis.getValue%20%3D%20function(input)%7B%20%0D%0Avar%20_in%20%3D%20input%3B%20%0D%0Avar%20inputList%20%3D%20document.getElementsByTagName(%22input%22)%3B%20%0D%0Athis.output%20%3D%20inputList%5B0%5D%3B%20%0D%0AregMethod%20%3D%20%2F%5E%5B%5C%2B%7C%5C-%7C%5C*%7C%5C%2F%5D%24%2Fi%3B%20%0D%0Aif(regMethod.test(_in))%7B%20%0D%0Athis.setMethod(_in)%3B%20%0D%0Areturn%3B%20%0D%0A%7D%20%0D%0AregNum%20%3D%20%2F%5E%5Cd%24%2Fi%3B%20%0D%0Aif(regNum.test(_in))%7B%20%0D%0Athis.setValue(_in)%3B%20%0D%0Areturn%3B%20%0D%0A%7D%20%0D%0AregSign%20%3D%20%2F%5E%5C%2B%5C%2F%5C-%24%2Fi%3B%20%0D%0Aif(regSign.test(_in))%7B%20%0D%0Athis.setSign(_in)%3B%20%0D%0Areturn%3B%20%0D%0A%7D%20%0D%0AregResult%20%3D%20%2F%5E%3D%24%2F%3B%20%0D%0Aif(regResult.test(_in))%7B%20%0D%0Athis.run()%3B%20%0D%0Areturn%3B%20%0D%0A%7D%20%0D%0A%7D%20%0D%0A%2F%2F%E8%AE%A1%E7%AE%97%E7%BB%93%E6%9E%9C%20%0D%0Athis.run%20%3D%20function(type)%7B%20%0D%0Aif(this.register.length%20%3C%202)%20return%20this.showmsg(1)%3B%20%0D%0Aif(this.register.length%20%3D%3D%202%20%26%26%20(this.tmp%20%3D%3D%20''))%20return%20this.showmsg(2)%3B%20%0D%0Aif(this.register.length%20%3D%3D%202%20%26%26%20this.tmp%20!%3D%20'')%7B%20%0D%0Athis.register%5B2%5D%20%3D%20this.tmp%3B%20%0D%0Athis.run()%3B%20%0D%0A%7D%20%0D%0A%0D%0Athis.showmsg(this.register.join('%20'))%3B%20%0D%0Avar%20_exp%20%3D%20parseInt(eval(this.register.join('%20')))%3B%20%0D%0Athis.output.value%20%3D%20_exp%3B%20%0D%0Afor(i%3D0%3Bi%3C3%3Bi%2B%2B)%7B%20%0D%0Athis.register.remove(i)%3B%20%0D%0A%7D%20%0D%0Athis.register%5B0%5D%20%3D%20_exp%3B%20%0D%0Athis.tmp%20%3D%20''%3B%20%0D%0Aif(type)%20this.register%5B1%5D%20%3D%20this.method%3B%20%0D%0Areturn%3B%20%0D%0A%7D%20%0D%0A%7D%20%0D%0Avar%20commonMath%20%3D%20new%20commonMath()%3B%20%0D%0AcommonMath.init()%3B%20%0D%0A%3C%2Fscript%3E"> <style> #main{ width:100%; height:100%; } #inputs{ position:relative; top:50%; left:50%; } .inbutton{ width:30px; } .outbutton{ width:145px; text-align:right; } .tcenter{ text-align:center; } </style> </head> <body> <div id="main"> <div> <span class="outbutton tcenter">etongchina@gmail.com</span> </div> <div> <span><input id="output" type="text" class="outbutton" maxlength="9" value="." /></span> </div> <div> <span><input type="button" class="inbutton" value="7" onclick="commonMath.getValue(this.value)" /></span> <span><input type="button" class="inbutton" value="8" onclick="commonMath.getValue(this.value)" /></span> <span><input type="button" class="inbutton" value="9" onclick="commonMath.getValue(this.value)" /></span> <span><input type="button" class="inbutton" value="+" onclick="commonMath.getValue(this.value)" /></span> </div> <div> <span><input type="button" class="inbutton" value="4" onclick="commonMath.getValue(this.value);" /></span> <span><input type="button" class="inbutton" value="5" onclick="commonMath.getValue(this.value);" /></span> <span><input type="button" class="inbutton" value="6" onclick="commonMath.getValue(this.value);" /></span> <span><input type="button" class="inbutton" value="-" onclick="commonMath.getValue(this.value);" /></span> </div> <div> <span><input type="button" class="inbutton" value="1" onclick="commonMath.getValue(this.value);" /></span> <span><input type="button" class="inbutton" value="2" onclick="commonMath.getValue(this.value);" /></span> <span><input type="button" class="inbutton" value="3" onclick="commonMath.getValue(this.value);" /></span> <span><input type="button" class="inbutton" value="*" onclick="commonMath.getValue(this.value);" /></span> </div> <div> <span><input type="button" class="inbutton" value="0" onclick="commonMath.getValue(this.value);" /></span> <span><input type="button" class="inbutton" value="+/-" onclick="commonMath.getValue(this.value);" /></span> <span><input type="button" class="inbutton" value="." onclick="commonMath.getValue(this.value);" /></span> <span><input type="button" class="inbutton" value="/" onclick="commonMath.getValue(this.value);" /></span> </div> <div> <span><input type="button" class="outbutton tcenter" value="计算" onclick="commonMath.getValue('=');" /></span> </div> </div> </body> </html>
[Ctrl+A 全选 注:
如需引入外部Js需刷新才能执行
]
教程首页
更多教程