function compute(obj)
{
obj.expr.value=eval(obj.expr.value);} 
为什么要加这个obj参数,这个事=号调用的函数为什么要加一个<form>标记?输入框<input> 那里的action是什么意思最后一个问题就是为什么1.2*6不是显示7.2而是显示7.199999999999999谢谢各位了最后想认识一点熟悉JavaScript的朋友 愿意教我的麻烦留一下QQ 万分感激!
以下就是本人写的计算机的代码,问题的代码已经用红色字体标记
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><script type="application/javascript">function compute(obj){ obj.expr.value=eval(obj.expr.value);}var one="1";var two="2";var three="3";var four="4";var five="5";var six="6";var seven="7";var eight="8";var nine="9";var zero="0";var jia="+";var jian="-";var cheng="*";var chu="/";var point=".";function enter(obj,string){ obj.expr.value+=string;}function clear(obj){ obj.expr.value="";}function tuige(obj){   var tt=obj.expr.value;   obj.expr.value=tt.substr(0,tt.length-1);}</script></head><body><form name="calc"><table border="1" background="JavaScript/图片/20070703095251427.jpg" align="center"><tr><td colspan="3"><input type="text" name="expr"  action="compute(this.form)" /></td><td>&nbsp;&nbsp;</td></tr><tr><td><input type="button" value="7" onclick="enter(this.form,seven)" /></td><td><input type="button" value="8" onclick="enter(this.form,eight)" /></td><td><input type="button" value="9" onclick="enter(this.form,nine)" /></td><td><input type="button" value="+" onclick="enter(this.form,jia)" /></td></tr><tr><td><input type="button" value="4" onclick="enter(this.form,four)" /></td><td><input type="button" value="5" onclick="enter(this.form,five)" /></td><td><input type="button" value="6" onclick="enter(this.form,six)" /></td><td><input type="button" value="-" onclick="enter(this.form,jian)" /></td></tr><tr><td><input type="button" value="1" onclick="enter(this.form,one)" /></td><td><input type="button" value="2" onclick="enter(this.form,two)" /></td><td><input type="button" value="3" onclick="enter(this.form,three)" /></td><td><input type="button" value="*" onclick="enter(this.form,cheng)" /></td></tr><tr><td><input type="button" value="0" onclick="enter(this.form,zero)" /></td><td><input type="button" value="." onclick="enter(this.form,point)" /></td><td colspan="2"><input type="button" value="/" onclick="enter(this.form,chu)" /></td></tr><tr><td><input type="button" value="=" onclick="compute(this.form)" /></td><td><input type="button" value="←" onclick="tuige(this.form)" /></td><td colspan="2"><input type="button" value="AC" onclick="clear(this.form)" /></td></tr></table></form></body></html>

解决方案 »

  1.   

    学艺不精,只能答上若干问题:
    为什么要加这个obj参数,这个事=号调用的函数
    obj是将form的dom对象传进来,eval执行这个表达式否则还是字符串,不是js所认识的表达式。
    为什么要加一个<form>标记?
    表示一个表单,之间的所有input以及button属于这个表单
    输入框<input> 那里的action是什么意思
    不懂,form有action,input的action头一次见到
    最后一个问题就是为什么1.2*6不是显示7.2而是显示7.199999999999999
    浮点数的精确度问题,这是基于IEEE754数值计算浮点数的通病,规避方法,确定你的数值的精确度,如加上toFixed(10),表示小数点后保留10位,四舍五入。