2010-03-07 51 views

回答

13

是的,它必須等待。 特別是,在其他腳本有機會執行之前,不能依賴jQuery(document).ready()來觸發。 ready綁定到DOMContentReady,readystatechanged或onload,無論哪個可用。

該文檔指出「在大多數情況下,腳本可以在DOM層次結構完全構建後立即運行」。請注意,的唯一保證是DOM在此事件觸發時準備就緒。 它不能保證你有任何其他 - 因爲它不能。

這一點,例如不會在IE,Firefox或鉻工作,brilliant.js總是ready()處理程序之前稱爲有機會執行不管你怎麼洗牌腳本標籤:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Test</title> 
    <script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.js" charset="utf-8" type="text/javascript" ></script> 
</head> 
<body> 
    <script type="text/javascript" > 
    // <![CDATA[ 
     alert("attaching event"); 
     $(document).ready(function() { alert("fired"); }); 
    // ]]> 
    </script> 
    <script type="text/javascript" src="brilliant.js" ></script> 
</body> 
</html> 

僅供參考,這裏是jQuery的1.4.2的相關代碼:

bindReady: function() { 
    if (readyBound) { 
     return; 
    } 

    readyBound = true; 

    // Catch cases where $(document).ready() is called after the 
    // browser event has already occurred. 
    if (document.readyState === "complete") { 
     return jQuery.ready(); 
    } 

    // Mozilla, Opera and webkit nightlies currently support this event 
    if (document.addEventListener) { 
     // Use the handy event callback 
     document.addEventListener("DOMContentLoaded", DOMContentLoaded, false); 

     // A fallback to window.onload, that will always work 
     window.addEventListener("load", jQuery.ready, false); 

    // If IE event model is used 
    } else if (document.attachEvent) { 
     // ensure firing before onload, 
     // maybe late but safe also for iframes 
     document.attachEvent("onreadystatechange", DOMContentLoaded); 

     // A fallback to window.onload, that will always work 
     window.attachEvent("onload", jQuery.ready); 

     // If IE and not a frame 
     // continually check to see if the document is ready 
     var toplevel = false; 

     try { 
      toplevel = window.frameElement == null; 
     } catch(e) {} 

     if (document.documentElement.doScroll && toplevel) { 
      doScrollCheck(); 
     } 
    } 
}, 
+1

請評論,如果你決定downvote。謝謝。 – 2010-03-07 19:54:39

2

我認爲$(document).ready()在html文檔被加載和渲染時運行。 閱讀文檔的詳細信息

http://docs.jquery.com/Tutorials:Introducing_$(document).ready()

+0

'就緒()'運行時,約翰Resig的決定它應該運行。它可以在不同的瀏覽器上運行,甚至可以在不同的時間在同一瀏覽器的不同版本上運行。在其他腳本有機會執行之前,您不能依賴它來觸發您的事件處理程序。您唯一可以肯定的是,當它觸發時您可以訪問DOM。 – 2010-03-07 20:26:35

2

第三方js文件可能會阻止,尤其是如果它在頭標記。嘗試將它放在<body>結束標記之前。

我認爲第一個答案是不正確的 - document.ready並不意味着所有內容都必須加載,這意味着dom是完整的。否則,在這裏運行的jquery方法將不會運行,直到所有圖像(例如)被加載,這是不正確的。

編輯

它看起來像行爲是腳本不同,但可以針對特定瀏覽器。這裏有一個很好的解釋:

JavaScript: DOM load events, execution sequence, and $(document).ready()

+1

根據Mozilla關於「DOMContentLoaded」事件的文檔,「ready」和「load」之間的關鍵區別在於頁面在調用「ready」之前並不等待圖像加載(即,「DOMContentLoaded」處理程序處於Firefox或Webkit)。不說有關腳本文件正在完成的任何內容。鑑於我們正在討論單線程模型,我不知道在瀏覽器仍然在解析/執行在結尾之前遇到的

  • 11. jQuery(document).ready()+等待焦點
  • 12. IE7 jQuery(document).ready()問題
  • 13. 一個或多個$(document).ready
  • 14. 刷新$(document).ready()
  • 15. 發行$(document).ready
  • 16. $(document).ready not working
  • 17. head.ready()vs $(document).ready
  • 18. JQuery:Rebind $(document).Ready()
  • 19. 由於$(document).ready
  • 20. $(document).ready和prototype.js
  • 21. GWT與$(document).ready
  • 22. 在$(document).ready
  • 23. window.open和$(document).ready
  • 24. $(document).ready shorthand
  • 25. jsdom和$(document).ready
  • 26. 正確使用jquery $(document).ready()聲明ready函數中調用的函數時
  • 27. 在$(document).ready之外使用jquery變量
  • 28. javascript作用域和jquery的$(document).ready
  • 29. 在$(document).ready()之外使用jQuery
  • 30. jQuery FancyBox Iframe - 在$ fancybox中使用$(document).ready?