栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

$(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 );}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377816.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号