2011-07-09 56 views
2

功能1我想知道這兩個函數有什麼區別?

// Toggle the sharing section 
    // Start with hiding the icon list 
    $("#shareThisPost").click(function(){ 
     $("#shareThisPost").fadeOut("slow"); 

     $(function() { 
      $("div#shareSection ul").fadeIn("slow"); 
      $shareState = 1; 
     }); 
    }); 

功能2

// Toggle the sharing section 
     // Start with hiding the icon list 
     $("#shareThisPost").click(function(){ 
      $("#shareThisPost").fadeOut("slow", function() { 
       $("div#shareSection ul").fadeIn("slow"); 
       $shareState = 1; 
      }); 
     }); 

回答

2
$("#shareThisPost").click(function(){ 
      $("#shareThisPost").fadeOut("slow", function() { 
       $("div#shareSection ul").fadeIn("slow"); 
       $shareState = 1; 
      }); 
     }); 

下面的代碼執行淡出發生後,這就是所謂的回調,

$("div#shareSection ul").fadeIn("slow"); 
       $shareState = 1; 
      }); 

這是equivelent到的document.ready,我不明白爲什麼它是一個點擊功能

$(function() { 
      $("div#shareSection ul").fadeIn("slow"); 
      $shareState = 1; 
     }); 
+0

這兩個函數都適用於我,但我只是想知道使用「,」與其他函數分開「慢」之間的區別是什麼。 – tanktery

4

第二將淡出結束後觸發。

首先是兩個單獨的函數首先點擊事件觸發,然後淡出開始,然後淡入開始。第二個點擊事件觸發。然後在淡出完成淡入開始後淡出。

正如你所說的,「,」是fadeOut函數的參數。想想這樣,如果你有一個函數定義

function x(param1, param2) { 
    //Do something 
} 

那麼淡出的結構就是這樣。

function fadeOut(param1, param2) { 
    // This time param2 is a call back function and param one is the speed fade. 
} 

在JS的功能可以被傳遞就像一個變量,所以param2的被作爲參數傳遞給淡出功能傳遞和被稱爲淡出內的淡出動作結束後。

在另一種情況下,fadeOut開始,然後調用下一個動作。所以,如果你真的想看看發生了什麼,試試用淡出時間(param1)1000來運行這兩個函數。這真的會讓你有時間看看兩者之間有什麼不同。

所以要回答你的問題,排序與「,」和沒有的區別之一是在一個功能被作爲參數傳遞到淡出,另一個函數運行,因爲它是自己的實體。

+0

爲什麼一個人使用「,」而另一個使用單獨的功能?他們都工作和做我想要他們,但我只想清楚我的代碼。 – tanktery