$(document).ready等同于没有jQuery

$(document).ready等同于没有jQuery,第1张

$(document).ready等同于没有jQuery

有一个基于标准的替代品,尽管IE8不

DOMContentLoaded
支持,但超过98%的浏览器都支持它:

document.addEventListener("DOMContentLoaded", function(event) {   //do work});

jQuery的本机功能比window.onload复杂得多,如下所示。

function bindReady(){    if ( readyBound ) return;    readyBound = true;    // Mozilla, Opera and webkit nightlies currently support this event    if ( document.addEventListener ) {        // Use the handy event callback        document.addEventListener( "DOMContentLoaded", function(){ document.removeEventListener( "DOMContentLoaded", arguments.callee, false ); jQuery.ready();        }, false );    // If IE event model is used    } else if ( document.attachEvent ) {        // ensure firing before onload,        // maybe late but safe also for iframes        document.attachEvent("onreadystatechange", function(){ if ( document.readyState === "complete" ) {     document.detachEvent( "onreadystatechange", arguments.callee );     jQuery.ready(); }        });        // If IE and not an iframe        // continually check to see if the document is ready        if ( document.documentElement.doScroll && window == window.top ) (function(){ if ( jQuery.isReady ) return; try {     // If IE is used, use the trick by Diego Perini     // http://javascript.nwbox.com/IEContentLoaded/     document.documentElement.doScroll("left"); } catch( error ) {     setTimeout( arguments.callee, 0 );     return; } // and execute any waiting functions jQuery.ready();        })();    }    // A fallback to window.onload, that will always work    jQuery.event.add( window, "load", jQuery.ready );}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4925160.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存