2012-10-18 41 views
0
<html> 
<head> 
<title> Buttons</title> 
<style type="text/css"> 

.intro{background-color:;} 
.duction{background-color:blue;} 
.function{background-color:grey;} 
.equals{background-color:orange;} 
</style> 





</head> 
<body> 
<form name="calculator"> 
<input type="text" name="display" length="50" width="100"> 
<div> 
<input type= "button" value="7" class="intro" id="7" onclick="one(7)"></button> 
<input type= "button" value="8" class="intro" id="8" onclick="one(8)"></button> 
<input type= "button" value="9" class="intro" id="9" onclick="one(9)"></button> 
<input type= "button" value="+" class="intro" id="+" onclick="one(+)"></button> 
<input type= "button" value="-" class="intro" id="-" onclick="one(-)"></button> 
<div> 
<input type= "button" value="4" class="intro" id="4" onclick="one(4)"></button> 
<input type= "button" value="5" class="intro" id="5" onclick="one(5)"></button> 
<input type= "button" value="6" class="intro" id="6" onclick="one(6)"></button> 
<input type= "button" value="x" class="intro" id="x" onclick="one(*)"></button> 
<input type= "button" value="/" class="intro" id="/" onclick="one(/)"></button> 
<div> 
<input type= "button" value="1" class="intro" id="1" onclick="one(1)"></button> 
<input type= "button" value="2" class="intro" id="2" onclick="one(2)"></button> 
<input type= "button" value="3" class="intro" id="3" onclick="one(3)"></button> 
<input type= "button" value="=" class="intro" ></button> 
<div> 
<input type= "button" value="0" class="intro" id="0" onclick="one(0)"></button> 
<input type= "button" value="." class="intro" id="." onclick="one(.)"></button> 
<input type= "button" value="c" class="intro" onclick="clearDigit()"></button> 

<script type="text/javascript" src="C:\Users\LS\Desktop\QBJS\button.js"> 



</script> 



</body> 
</html> 

的Javascript無法添加數字來我的計算器顯示屏

function one(event) 
{ 
clearTimeout(timer); 
timer=setTimeout("AddDigit(object)",500); 
object=event; 
} 

//每次我鍵入一個數字我的顯示窗口它取代了以前數而不將它像一個實際的計算器會。也試圖弄清楚爲什麼我不能得到出現在我的顯示窗口我的函數符號....我懷疑它是與我的setTimeout ....

function AddDigit(x) 
    { 
    object=x; 
    if (eval(digit) == 0) 
    {digit = object;} 
    else 
    {digit = digit + object;} 

document.calculator.display.value=object; 

} 
+0

爲什麼使用計時器而不是立即調用AddDigit? – PherricOxide

回答

0

它不串聯,它甚至沒有添加。你不能像那樣通過。原因是當你用一個字符串發出回調時,它只會調用該函數。如果你想傳遞參數,那麼你需要創建一個閉包並將它們傳遞給那裏。

試試這個:

timer=setTimeout(function(){ AddDigit(object); },500); 

更可能你的意思傳遞是事件:

timer=setTimeout(function(){ AddDigit(event); },500); 

object = event; 
timer=setTimeout(function(){ AddDigit(object); },500); 

此外,在AddDigit,有可能你想改爲:

document.calculator.display.value += object; 
0

你這樣做,

document.calculator.display.value=object; 

但級聯的版本是 「數字」 變量。