2011-06-07 75 views
0

我想嘗試在啓用它時添加到某個變量,並且當它被禁用時,它將從變量中減去,但是當我alert變量的值剛剛出現時說NaN這裏是我的代碼:試圖增加/減少變量

這是啓用/禁用按鈕

var speedrating; 
function onoffButton(i, g, r) 
{ 
    $(i).click(function() { 
     if ($(i).html() == '<a class="onoffswitch" id="' + g + 'ON"></a>') { 
      $(i).html('<a class="onoffswitch" id="' + g + 'OFF"></a>'); 
      speedrating -= r; 
     } else { 
      speedrating += r; 
      $(i).html('<a class="onoffswitch" id="' + g + 'ON"></a>'); 
     } 
     updateSpeedRate(); 
    }); 
} 

這裏是它檢查變量,看看做這件事:

function updateSpeedRate() 
{ 
    if (speedrating < 34) { 
     $('.speedmeter').css('background-position', '0px -30px'); 
    } else { 
     if (speedrating < 67) { 
      $('.speedmeter').css('background-position', '0px -15px'); 
     } else { 
      $('.speedmeter').css('background-position', '0px 0px'); 
     } 
    } 
    alert(speedrating); 
} 

然後使用啓用/禁用對象(第三個數字是我想與之合作的號碼):

onoffButton("#switchRating", "rating", 10); 

onoffButton("#switchFavicon", "favicon", 30); 

onoffButton("#switchClock", "clockswitch", 10); 

onoffButton("#switchWeather", "weatherswitch", 30); 

onoffButton("#switchNotepad", "notepadswitch", 15); 

onoffButton("#switchLinks", "linksswitch", 5); 

回答

6

你需要給你加之前speedRating值/從中

var speedrating = 0;

+0

它的工作原理!謝謝! – Erurainion 2011-06-07 02:29:16