jquery如何实现domReady和onload判断的

function ready(fn) {

    var completed = function() {



        if ( document.addEventListener ) {

            document.removeEventListener( "DOMContentLoaded", completed, false );

            window.removeEventListener( "load", completed, false );



        } else {

            document.detachEvent( "onreadystatechange", completed );

            window.detachEvent( "onload", completed );

        }



        fn();

    }

    if (document.readyState === 'completed') { // 页面本身已加载结束

        fn();

    } else if (document.addEventListener) {



        document.addEventListener("DOMContentLoaded", completed, false);

        window.addEventListener("load", completed, false);

    } else {

        document.attachEvent("onreadystatechange", completed);

        window.attachEvent("load", completed);

    }

}



ready(function() {

    console.log(1);

});



console.log(3);

window.onload = function() {

    console.log(2);

}

console.log(4);





// 3,4,1,2

 

你可能感兴趣的:(jquery)