首頁  >  文章  >  web前端  >  jquery中函數ready用法分析

jquery中函數ready用法分析

巴扎黑
巴扎黑原創
2017-06-25 10:17:441428瀏覽

 最近看一些關於jquery ready 有人說他緩慢,有人說他快,說法不一。 於是自己深入研究一下。首先看了一下jquery 文件 關於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事件,当页面渲染完成之后会执行这个函数,在所以元素加载完成之前,这个函数不会被调用,例如图像。但是在大多数情况下,只要DOM结构加载完,脚本就可以尽快运行。传递给.ready()的事件句柄在DOM准备好后立即执行,因此通常情况下,最好把绑定事件句柄和其他jQuery代码都到这里来。但是当脚本依赖于CSS样式属性时,一定要在脚本之前引入外部样式或内嵌样式的元素。   
  
如果代码依赖于需加载完的元素(例如,想获取一个图片的尺寸大小),应该用.load()事件代替,并把代码放到load事件句柄中。
# 依照文件上面的說明,頁面內有大量文件結構,圖片資源時候,ready 是快於load 的。文件裡面也清楚的分析了什麼時候用ready 什麼時候用load。

下面分析一下jquery ready 的運作流程


$(handler) or $(document).ready(handler) →  ready() → bindReady() → 执行readyList.add( fn ) fn
 大致看一下原始碼

 下面是jquery 的

物件的ready 原始碼

 


 jQuery.fn = jQuery.prototype = {   
            constructor: jQuery,   
            init: function( selector, context, rootjQuery ) {   
                // HANDLE: $(function)   
                // Shortcut for document ready   
                // 如果函数,则认为是DOM ready句柄   
                if ( jQuery.isFunction( selector ) ) {   
                    return rootjQuery.ready( selector );   
                }   
            },   
           
            ready: function( fn ) {   
                // Attach the listeners   
                jQuery.bindReady(); // 绑定DOM ready监听器,跨浏览器,兼容标准浏览器和IE浏览器   
           
                // Add the callback   
                     readyList.add( fn );// 将ready句柄添加到ready异步句柄队列   
           
                return this;   
            }   
        };
 

 呼叫jquery 的 bindReady ,  增加ready回呼!

  下面看一下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已经完毕,立即调用jQuery.ready   
                if ( document.readyState === "complete" ) {   
                    // Handle it asynchronously to allow scripts the opportunity to delay ready   
                    // 重要的是异步   
                    return setTimeout( jQuery.ready, 1 );   
                }   
            //下面是一些防御性的编程 故此省略        ......
}
 這個應該很清楚  document.readyState == 'complete' 就會執行jquery 的ready ,我很困惑的是為什麼是setTiemout(jQuery.ready,1) ,請回上面看ready 的程式碼, readyList.add( fn ), 如果不是異步的,執行回呼的就會放到 readyList.add( fn )之前了,因為執行是在jQuery 的ready 裡面 readyList.fireWith( document, [ jQuery ] );readylist 是jquery 的callbacks ,就是管理

回呼函數的!不清楚的可以看看文件。

註:你會發現有兩個ready,這兩個是不同的,一個放到jquery.prototype 就是我們$(doucument).ready這個,另一個是jquery的物件方法判斷是否已經ready了的方法

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

 

 

 

 

以上是jquery中函數ready用法分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn