標籤:ntc res 協議 event turn var loaded 任務 return
1、window.onload在$(document).ready()之後執行
原因:二者觸發與否,都是依據document.readyState這個屬性。
(1)document.readyState的值:uninitialized、loading、interactive、complete(這四個值,依據時間先後排序)。
(2)當其值為interactive時,會觸發documentContentLoaded事件,JQuery.ready()就是對documentContentLoaded事件設計了一個函數,從而執行JQuery.ready定義的任務隊列。
(3)當其值為complete時,會觸發load事件。
(4)綜上,window.onload會在$(document).ready()之後執行。
協議原文:document . readyState
Returns "loading" while the Document is loading, "interactive" once it is finished parsing but still loading sub-resources, and "complete" once it has loaded.
The readystatechange event fires on the Document object when this value changes.
2、window.onload多次賦值會產生覆蓋現象,而$(document).ready()不會。
原因:
(1)onload是window對象的成員方法,是指向堆記憶體中的Function類型的Object的引用。因為JavaScript不存在函數重載機制,所以對onload多次賦值,會導致解除上一次對某Function的引用,轉而將引用指向當前函數的引用,從而產生覆蓋現象。
(2)$(document).ready()實現了一個任務對象,多次傳入function,向任務隊列中添加一個成員而已。
JavaScript tips:window.onload與$(document).ready()的差異