<html>
<head>
<title>
文本相关标记的应用</title>
<style type="text/css">
.bgcolor
{
background-color:#CCCCCC
}</style>
<script language="javascript">
var total =0;
var flagNew=false;//是否是新增的数字
var operation="";//以id 号来确定用户输入的运算符
//获取用户输入的运算符 
function Operation(ope)
{
   var ShowOut=document.getElementById("count").value;    
       flagNew = true;
       if ( '+' == operation )
       {
      total = parseFloat(total)  +  parseFloat(ShowOut);
      alert(total);
    }
       else if ( '-' == operation )
       {
      total -= ShowOut;
    }
       else if ( '/' == operation )
       {
      total /= ShowOut;
    }
       else if ( '*' == operation )
       {
      total *= ShowOut;
     }
       else
       {
      total =ShowOut;
  }
       document.getElementById("count").value = total;
       operation = ope;
   
}
//获取用户按按钮的值 
function GetInValue(num)
{
   if(flagNew)
     {
     document.getElementById("count").value=num;
     flagNew=false;
  }
   else
      {
      if( document.getElementById("count").value==0)
      {
        document.getElementById("count").value=num;
      }
     else
        {
       document.getElementById("count").value +=num;
     }
   }
  
}//清零设置
function ClearEmpty () 
{
document.getElementById("count").value = "0";
flagNew = true;
}     
</script></head>
<body  text="#000000" bgcolor="FFFFFF">
<table width="222" height="40" border="1" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td colspan="4" width="300" height="16" ><input type="text" id="count" value="" width="300" height="16" align="middle"/>&nbsp;</td>
  </tr>
  <tr>
    <td  ><input type="button" id="7" width="35" height="15" value="7" class="bgcolor" onClick="GetInValue(7)" />&nbsp;</td>
    <td ><input type="button" id="8" value="8" width="35" height="15" class="bgcolor" onClick="GetInValue(8)"/>&nbsp;</td>
    <td ><input type="button" id="9" value="9" width="35" height="15" class="bgcolor" onClick="GetInValue(9)"/>&nbsp;</td>
    <td ><input type="button" id="+" value="+" width="35" height="15" class="bgcolor" onClick="Operation('+')"/>&nbsp;</td>
  </tr>
  <tr>
    <td><input type="button" id="4" value="4" width="35" height="15" class="bgcolor" onClick="GetInValue(4)"/>&nbsp;</td>
    <td><input type="button" id="5" value="5" width="35" height="15" class="bgcolor" onClick="GetInValue(5)"/>&nbsp;</td>
    <td><input type="button" id="6" value="6" width="35" height="15" class="bgcolor" onClick="GetInValue(6)"/>&nbsp;</td>
    <td><input type="button" id="-" value="-" width="35" height="15" class="bgcolor" onClick="Operation('-')"/>&nbsp;</td>
  </tr>
  <tr>
    <td><input type="button" id="1" value="1" width="35" height="15" class="bgcolor" onClick="GetInValue(1)"/>&nbsp;</td>
    <td><input type="button" id="2" value="2" width="35" height="15" class="bgcolor" onClick="GetInValue(2)"/>&nbsp;</td>
    <td><input type="button" id="3" value="3" width="35" height="15" class="bgcolor" onClick="GetInValue(3)"/>&nbsp;</td>
    <td><input type="button" id="*" value="*" width="35" height="15" class="bgcolor" onClick="Operation('*')"/>&nbsp;</td>
  </tr>
  <tr>
    <td><input type="button" id="0" value="0" width="35" height="15" class="bgcolor" onClick="GetInValue(0)"/>&nbsp;</td>
    <td><input type="button" id="c" value="c" width="35" height="15" class="bgcolor" onClick="ClearEmpty () "/>&nbsp;</td>
    <td><input type="button" id="/" value="/" width="35" height="15" class="bgcolor" onClick="Operation('/')"/>&nbsp;</td>
    <td><input type="button" id="=" value="=" width="35" height="15" class="bgcolor" onClick="Operation('=')"/>&nbsp;</td>
  </tr>
</table>
</body>
</html>
各位帮我看下啊,total没有给他值,怎么就可以加了呢?加号那里alert()了,为什么要点=号以后才会弹出呢

解决方案 »

  1.   

    function GetInValue(num)
    {
      if(flagNew)
      {
      document.getElementById("count").value=num;
      flagNew=false;
      }
      else
      {
      if( document.getElementById("count").value==0)
      {
      document.getElementById("count").value=num;
      }
      else
      {
      document.getElementById("count").value = document.getElementById("count").value +=num;
      }
      }
      
    }
      

  2.   

    因为第一次点+号的时候,operation 还是空,所以这时候是不会alert的.执行到最后才赋的值.