ホームページ  >  記事  >  ウェブフロントエンド  >  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 をいつ使用するかを明確に分析しています。

jquery Readyの実行プロセスを分析しましょう


$(handler) or $(document).ready(handler) →  ready() → bindReady() → 执行readyList.add( fn ) fn

ソースコードを簡単に見てみましょう

以下は、jqueryのオブジェクト


 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' が実行されます。これが setTiemout(jQuery.ready,1 ) の理由です。上記の Ready コード、readyList.add( fn ) に戻ってください。非同期でない場合、コールバックの実行は、readyList.add( fn ) の前に配置されます。実行は jQuery の Ready ReadyList.fireWith( document, [ jQuery ] );readylist で行われます。readylist は、コールバック関数

を管理する jquery のコールバックです。よくわからない場合は、ドキュメントを読んでください。 注: 準備ができているものが 2 つあることがわかります。これら 2 つは異なります。1 つは jquery.prototype ($(document).ready) に配置され、もう 1 つは準備ができているかどうかを判断するための jquery のオブジェクト メソッドです

追記:jqueryは奥が深くて記事内に間違いがあれば修正してください!

以上がjqueryの関数readyの使い方の解析の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。