jQuery的domReady

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>jQuery的DOMready</title>
        <script>
            var domReady = function(fn){
                var completed = function() {
                    document.removeEventListener( "DOMContentLoaded", completed, false );
                    window.removeEventListener( "load", completed, false );
                    fn();
                };
                
                // 如果dom已经准备好了,那就开始咯
                if ( document.readyState === "complete" ) {
                    setTimeout( domReady,0,fn );
    
                } else {
                    document.addEventListener( "DOMContentLoaded", completed, false );
                    // 保险起见加个load事件咯
                    window.addEventListener( "load", completed, false );
                }
                
                
            }
            domReady(function(){
                alert(document.querySelectorAll(".test")[0].innerHTML);
            });
        </script>
    </head>
    <body>
        jQuery的DOMready
        <div class="test">dom ready?</div>
    </body>
    <script>
            
        
        
        
    </script>
</html>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。