$(document).ready(function() { // Handler for .ready() called. });
jQuery 3.0 ready() 變化
在jQuery 3.0發(fā)布之前,有以下幾種方法稱之為ready方法:
在document元素上操作: $(document).ready(handler);
在空元素上操作: $().ready(handler);
或者直接(即不在一個(gè)具體的元素上)操作: $(handler);
上述所有命名的變種在功能上是等價(jià)的。無論是哪個(gè)元素,在DOM加載完畢之后其指定的處理程序都將會被調(diào)用。換句話說,這里的DOM加載完畢并不表示在文檔中的某個(gè)具體的元素,比如img元素,加載完畢。相反,這里表示的是整個(gè)DOM樹加載完畢。
在jQuery 3.0中,除了$(handler) 其他的ready方法都被棄用。
官方聲明為此:
這是因?yàn)檫x擇器并沒有和ready()建立聯(lián)系,不僅低效而且會導(dǎo)致瀏覽器引擎對該方法的行為進(jìn)行不正確的假設(shè)。
ready 事件和 load 事件的區(qū)別
當(dāng)DOM加載完畢且元素能夠被安全訪問時(shí)就會觸發(fā)ready事件。另一方面,load事件卻在DOM和所有資源加載后觸發(fā)。
可以像下面這樣使用load事件:
$(window).on("load", function(){ // Handler when all assets (including images) are loaded });
這樣的話,不僅僅要等到DOM結(jié)構(gòu)能完全訪問,而且還需要等到所有的圖片資源完全加載完畢(加載時(shí)間取決于圖片文件大小)才能執(zhí)行函數(shù)。
正常的DOM操作你可能不需要load事件,但是如果你想要在所有的資源被加載完畢之前展示一個(gè)旋轉(zhuǎn)的加載器樣式,比如,又或者你想要用JS計(jì)算一下圖片的大小,這可能是一個(gè)好的選擇。
你可能不需要jQuery.ready()
ready 方法可以確保代碼只在所有DOM元素能被安全操縱時(shí)才執(zhí)行。 但這意味著什么呢?這意味著當(dāng)你要執(zhí)行的js代碼嵌在HTML中某個(gè)片段中時(shí),瀏覽器也要加載完以下元素才能執(zhí)行。
就像下面這個(gè)例子一樣:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>.ready() tutorial</title> <script src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script> <script> $(function(){ // .ready() callback, is only executed when the DOM is fully loaded var length = $("p").length; // The following will log 1 to the console, as the paragraph exists. // This is the evidence that this method is only called when the // DOM is fully loaded console.log(length); }); </script> </head> <body> <p>I'm the content of this website</p> </body> </html>
如果你要執(zhí)行的javascript代碼放在body末尾,你就可能不需要使用ready()方法,因?yàn)闉g覽器解析到j(luò)avascript時(shí)你可能試圖操縱和訪問的DOM元素已經(jīng)被加載完了:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>.ready() tutorial</title> </head> <body> <p>I'm the content of this website</p> <script src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script> <script> var length = $("p").length; // The following will log 1 to the console, as the paragraph exists. console.log(length); </script> </body> </html>
原生JavaScript ready()替代
對于現(xiàn)代瀏覽器以及IE9+,你可以通過監(jiān)聽 DOMContentLoaded 事件實(shí)現(xiàn)ready()相同的功能:
document.addEventListener("DOMContentLoaded", function(){ // Handler when the DOM is fully loaded });
但是,請注意,如果事件已經(jīng)發(fā)射,回調(diào)將不會被執(zhí)行。為了確保回調(diào)總是運(yùn)行,jQuery檢查文檔reference)的“readyState”屬性,如果屬性值變?yōu)?complete,則立即執(zhí)行回調(diào)函數(shù):
var callback = function(){ // Handler when the DOM is fully loaded }; if ( document.readyState === "complete" || (document.readyState !== "loading" && !document.documentElement.doScroll) ) { callback(); } else { document.addEventListener("DOMContentLoaded", callback); }
包括domReady庫,已經(jīng)實(shí)現(xiàn)了這個(gè)解決方案。
老版本的IE瀏覽器
對于IE8及以下的瀏覽器,你能使用onreadystatechange 事件去監(jiān)聽文檔的readyState 屬性:
document.attachEvent("onreadystatechange", function(){ // check if the DOM is fully loaded if(document.readyState === "complete"){ // remove the listener, to make sure it isn't fired in future document.detachEvent("onreadystatechange", arguments.callee); // The actual handler... } });
或者你可以使用Load事件,如jQuery,這樣可以在任何瀏覽器上運(yùn)行。這也會導(dǎo)致一個(gè)時(shí)間延遲,因?yàn)樗鼤却械馁Y產(chǎn)被加載。
注意,在這個(gè)解決方案中你也要檢查readyState,如上文所述,這樣能確保回調(diào)總是能夠被執(zhí)行。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com