2011-09-12 39 views
0
<html> 

<head> 
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"> 
<title>Vote!</title> 

<script type="text/javascript"> 
var x = 0; 
function countClicks() { 



x += 1 
document.getElementById("counting").innerHTML = x; 

    ClickCount++; 
    return true; 
} 

</script> 

<script type="text/javascript"> 
var x = 0; 
function countClicks1() { 
x += 1 
document.getElementById("counting1").innerHTML = x; 

    ClickCount++; 
    return true; 
} 

</script> 

<script type="text/javascript"> 
var x = 0; 
function countClicks2() { 



x += 1 
document.getElementById("counting2").innerHTML = x; 

    ClickCount++; 
    return true; 
} 

</script> 

</head> 
<body> 

<div id="chart1"> 
<ul> 
<li> 
<img src="../Pictures/BWS + L.A +KUSH/Game.RED_Album_Cover.jpg" alt="red album"><br> 
<input type="button" value="VOTE" name="clickOnce" onclick="return countClicks();" /> 
<div id="counting"></div> 
</li> 
<li> 
<img src="../Pictures/BWS + L.A +KUSH/Game.RED_Album_Cover.jpg" alt="red album"><br> 
<input type="button" value="VOTE" name="clickOnce" onclick="return countClicks1();" /> 
<div id="counting1"></div> 
</li> 
<li> 
<img src="../Pictures/BWS + L.A +KUSH/Game.RED_Album_Cover.jpg" alt="red album"><br> 
<input type="button" value="VOTE" name="clickOnce" onclick="return countClicks2();" /> 
<div id="counting2"></div> 
</li> 
</ul> 
</div> 

</body> 

</html> 

回答

0

var x是一個全局變量。一旦它被聲明,那麼你的函數將使用相同的變量。因此,在countClickscountClicks1中,它們都使用相同的變量。

更改第二個使用var x2 = 0

1

嗯...

因爲x是一個全局變量的clickCount總是使用相同的變量...

還,我建議你幹你的代碼,並進行功能操作基於一個參數,你不需要3個功能都完全相同的事情。

相關問題