2013-03-12 82 views
2

我想製作一個盒子,但是當點擊一個列表項時,它會下降,然後單擊時返回。我之前做了很多次,但由於某種原因,jquery添加了display:none;切換添加顯示無?

 $('li.item-162').toggle(function(){ 
          $('.login').animate({ 
           top: '27px' 
           }, 1000); 

         },function() {   
          $('.login').animate({ 
           top: '-212px' 
           }, 1000); 
         }); 

     $(".li.item-162").show(); 

而且在螢火我的代碼顯示 enter image description here 你可以看到它住在這裏.. http://rdhealthandsafety.co.uk/index.php

任何想法,爲什麼發生這種情況,我想它我週二上午藍調:(

+2

其實這就是['.toggle ()'](http://api.jquery.com/toggle/)會:「*顯示或隱藏匹配的元素。*」。 – insertusernamehere 2013-03-12 11:44:48

+0

你究竟想要什麼? – supersaiyan 2013-03-12 11:46:29

+1

我認爲切換是用來作爲關閉? – Brent 2013-03-12 12:15:36

回答

6

您打算使用.toggle()函數的方式是已棄用jQuery 1.8已被刪除jQuery 1.9

Category: Deprecated 1.8

.toggle()綁定兩個或更多個處理程序來匹配的元素,以在交替的點擊來執行。

這理由在Changes of Note in jQuery 1.9

這是.toggle的 「點擊元素運行指定的功能」 簽名()。不應該將它與「.toggle()」的「更改元素的可見性」相混淆,這不會被廢棄。前者正在被刪除,以減少混淆,並提高圖書館模塊化的潛力。 jQuery Migrate插件可用於恢復功能。

然而Gaby aka G. Petrioli創建,做同樣的回答的功能What to use instead of toggle(…) in jQuery > 1.8?

$.fn.toggleClick = function(){ 
    var methods = arguments, // store the passed arguments for future reference 
     count = methods.length; // cache the number of methods 

    //use return this to maintain jQuery chainability 
    return this.each(function(i, item){ 
     // for each element you bind to 
     var index = 0; // create a local counter for that element 
     $(item).click(function(){ // bind a click handler to that element 
      return methods[index++ % count].apply(this,arguments); // that when called will apply the 'index'th method to that element 
      // the index % count means that we constrain our iterator between 0 and (count-1) 
     }); 
    }); 
}; 

您可以簡單地使用這樣的:

$('selector').toggleClick(function1, function2, […]);