jQuery DOM加载的三种写法

$(function(){

});

$(document).ready(function(){

});

$(document).on('ready', function(){

});

这三种写法本质都是调用了$().ready()实例方法,ready()是写在jQuery.prototype中的方法

ready: function( fn ) {
     // Add the callback
     jQuery.ready.promise().done( fn );

     return this;
}

jQuery.ready.promise中调用了 DOMContentLoaded 和 load事件,当DOM加载完成就会触发这个事件。

DOM加载完成的标志是 document.readyState === 'complete';


<script type="text/javascript" src="https://code.jquery.com/jquery-2.0.3.js"></script>


猜你喜欢

转载自blog.csdn.net/jerny2017/article/details/80402856