2010-07-10 23 views

回答

6

沒有。 jQuery事件堆疊在一起,並將逐個執行。

從jQuery docs on bind()

當一個事件到達的元素,綁定到該事件類型的元件的所有處理程序被解僱。如果有多個處理程序註冊,它們將始終按照它們綁定的順序執行。

1

編號jQuery的目的是堆疊它們。

6

不會有衝突。

$(document).ready(function() { alert("One"); }); 
$(document).ready(function() { alert("Two"); }); 

將提醒兩次。

4
$(document).ready() 

非常安全 - 您可以掛鉤多個處理程序或在事件已經被觸發很長時間後使用它。

如果調試到jQuery的(只包括unminified版本),你會看到.ready()樣子:

ready: function(fn) { 
    // Attach the listeners 
    jQuery.bindReady(); 

    // If the DOM is already ready 
    if (jQuery.isReady) { 
     // Execute the function immediately 
     fn.call(document, jQuery); 

    // Otherwise, remember the function for later 
    } else if (readyList) { 
     // Add the function to the wait list 
     readyList.push(fn); 
    } 

    return this; 
} 

所以,如果jQuery.isReady(指示文件準備已經發生),那麼$(文件).ready()函數被立即調用。否則,該函數將被添加到一個處理程序數組中。

內部jQuery的自己ready處理程序:

 // If there are functions bound, to execute 
     if (readyList) { 
      // Execute all of them 
      var fn, i = 0; 
      while ((fn = readyList[ i++ ])) { 
       fn.call(document, jQuery); 
      } 

      // Reset the list of functions 
      readyList = null; 
     } 

因此,在ready jQuery的遍歷,並調用它註定要ready的所有功能。這些函數被串行調用(一個接一個地,不是並行地)。這意味着一個人會在下一個開始之前完成。

+0

+1,用於更好,更徹底的解釋。 – 2010-07-10 16:08:33

相關問題