2013-02-10 29 views
0

比方說,我有一個簡單的頁面,只包含,單擊時按鈕加1,Java腳本num變量:如何在HTML頁面中顯示變化的變量(如數字)?

HTML:

<body> 
<div> 
<button type="button" onclick="someFunction()">Clicking here adds 1 to the "count" variable</button> 
</div> 
<br> 
<div id="output"></div> 
</body> 

的Javascript:

var count = new num; 

someFunction() { 
    count =+ 1; 
} 

所以我問題是你會怎樣把js顯示在ID爲「output」的div中的「count」變量?你使用innerHTML嗎?因爲這似乎並沒有工作....

此外,是否有可能有或沒有jQuery?

回答

0

爲什麼不呢?但是,您必須注意,數字增量必須使用count += 1或更短的count++完成。在下面的例子中,我已經使用前綴增量在賦值之前進行計算。

var count = 0; 
function someFunction() { 
    document.getElementById("output").innerHTML = ++count; 
} 

另外,考慮用上述的初始值初始化數值變量。

+0

只是試了一下,它的工作。我看到我搞砸了......我沒有告訴它正確顯示變量。 – user2059701 2013-02-10 22:20:53