2013-02-07 52 views
0

嘿傢伙,我知道存在.slideToggle()但我想稍後添加更多功能。爲什麼if/else語句總是執行if分支?

我不知道我在做什麼錯,滑動的作品,但我不能滑下來。

我可以不覆蓋我的var嗎?當有人能幫助我時會很好。

$(document).ready(function() { 

    var resizeValue = true; 
    $(".resizeSelect").click(function() { 
     if (resizeValue === true) { 
      $(".resize").slideUp(

      function() { 
       $('.parent').height($('.child').height('100')); 
      }); 
      var resizeValue = false; 
     } else { 
      $(".resize").slideDown(

      function() { 
       $('.parent').height($('.child').height('100')); 
      }); 
      var resizeValue = true 
     }; 
    }); 
}); 
+1

你試圖把一些CONSOL /警報日誌,看看那裏的代碼,掛起? – Dukeatcoding

回答

8

您不應該在click函數中重新定義resizeValue變量。只需從var resizeValue中刪除var(它只能用於ready功能的頂部)。

+0

這指向這個關於在JS中傳遞引用的答案:http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language – Alfabravo

+0

OMG ^^非常感謝。 – qudrat

6

因爲你重新聲明你的變量resizeValue在你的函數,而不是更新:

$(document).ready(function() { 

    var resizeValue = true; 
    $(".resizeSelect").click(function() { 
     if (resizeValue === true) { 
      $(".resize").slideUp(

      function() { 
       $('.parent').height($('.child').height('100')); 
      }); 
      //DO NOT DECLARE NEW VARIABLE WITH VAR 
      resizeValue = false; 
     } else { 
      $(".resize").slideDown(

      function() { 
       $('.parent').height($('.child').height('100')); 
      }); 
      //DO NOT DECLARE NEW VARIABLE WITH VAR 
      resizeValue = true 
     }; 
    }); 
}); 
+0

謝謝你的回答:) – qudrat