国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

jquery ready函數(shù)深入分析

2020-01-26 14:26:51
字體:
供稿:網(wǎng)友

最近看一些關(guān)于jquery ready 有人說他緩慢,有人說他快,說法不一。 于是自己深入研究一下。首先看了一下jquery 文檔 關(guān)于ready 的描述

While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received. In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed. The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code. When using scripts that rely on the value of CSS style properties, it's important to reference external stylesheets or embed style elements before referencing the scripts.In cases where code relies on loaded assets (for example, if the dimensions of an image are required), the code should be placed in a handler for the load event instead.

翻譯一下

雖然JavaScript提供了load事件,當(dāng)頁面渲染完成之后會執(zhí)行這個函數(shù),在所以元素加載完成之前,這個函數(shù)不會被調(diào)用,例如圖像。但是在大多數(shù)情況下,只要DOM結(jié)構(gòu)加載完,腳本就可以盡快運(yùn)行。傳遞給.ready()的事件句柄在DOM準(zhǔn)備好后立即執(zhí)行,因此通常情況下,最好把綁定事件句柄和其他jQuery代碼都到這里來。但是當(dāng)腳本依賴于CSS樣式屬性時,一定要在腳本之前引入外部樣式或內(nèi)嵌樣式的元素。  
 
如果代碼依賴于需加載完的元素(例如,想獲取一個圖片的尺寸大小),應(yīng)該用.load()事件代替,并把代碼放到load事件句柄中。    

依照文檔上面的說明,在頁面內(nèi)有大量文檔結(jié)構(gòu),圖片資源時候,ready 是快于 load 的。文檔里面也清晰的分析了什么時候用ready 什么時候用load。

下面分析一下jquery ready 的運(yùn)行流程

$(handler) or $(document).ready(handler) →  ready() → bindReady() → 執(zhí)行readyList.add( fn ) fn

 大致看一下源碼

 下面是jquery 的 對象的 ready 源碼

 jQuery.fn = jQuery.prototype = {        constructor: jQuery,        init: function( selector, context, rootjQuery ) {          // HANDLE: $(function)          // Shortcut for document ready          // 如果函數(shù),則認(rèn)為是DOM ready句柄          if ( jQuery.isFunction( selector ) ) {            return rootjQuery.ready( selector );          }        },              ready: function( fn ) {          // Attach the listeners          jQuery.bindReady(); // 綁定DOM ready監(jiān)聽器,跨瀏覽器,兼容標(biāo)準(zhǔn)瀏覽器和IE瀏覽器                // Add the callback             readyList.add( fn );// 將ready句柄添加到ready異步句柄隊列                return this;        }      };  

 調(diào)用jquery 的 bindReady ,  增加ready回調(diào)!

  下面看一下 bindReady 大致源碼

bindReady: function() { // jQuery.bindReady          if ( readyList ) {            return;          }           readyList =jQuery.Callbacks( "once memory" )// 初始化ready異步事件句柄隊列           // Catch cases where $(document).ready() is called after the          // browser event has already occurred.          // 如果DOM已經(jīng)完畢,立即調(diào)用jQuery.ready          if ( document.readyState === "complete" ) {            // Handle it asynchronously to allow scripts the opportunity to delay ready            // 重要的是異步            return setTimeout( jQuery.ready, 1 );          }        //下面是一些防御性的編程 故此省略    ......}

   這個應(yīng)該很清楚  document.readyState == 'complete' 就會 執(zhí)行 jquery 的 ready ,我很困惑的是為什么是 setTiemout(jQuery.ready,1) ,請返回上面看ready 的代碼, readyList.add( fn ), 如果不是異步的,執(zhí)行回調(diào)的就會放到 readyList.add( fn )之前了,因為執(zhí)行是在jQuery 的ready 里面 readyList.fireWith( document, [ jQuery ] );readylist 是jquery 的callbacks ,就是管理回調(diào)函數(shù)的!不清楚的可以看看文檔。

注:你會發(fā)現(xiàn)有兩個ready,這兩個是不同的,一個放到 jquery.prototype 就是我們$(doucument).ready這個,另一個是jquery的對象方法判斷是否已經(jīng)ready了的方法

ps : jquery博大精深,文章有錯誤之處,還請各位指正!

 以上就是對 jquery ready的資料整理,后續(xù)繼續(xù)整理相關(guān)資料,謝謝大家對本站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 富裕县| 安阳县| 肃北| 保定市| 彝良县| 秦安县| 马关县| 溧水县| 赣榆县| 永年县| 齐齐哈尔市| 吉木萨尔县| 东平县| 恩施市| 西乡县| 二手房| 华池县| 冷水江市| 彭阳县| 磴口县| 彭水| 洱源县| 西宁市| 襄垣县| 安义县| 宣恩县| 宜兴市| 申扎县| 新化县| 商城县| 东海县| 邵东县| 荥经县| 汉源县| 鄄城县| 五家渠市| 获嘉县| 讷河市| 正阳县| 阿荣旗| 大足县|