您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

jquery事件的ready()方法使用详解_jquery

2025/3/4 1:58:40发布18次查看
页面初始化中,用的较多的就是$(document).ready(function(){//代码}); 或 $(window).load(function(){//代码});
他们的区别就是,ready是在dom的结构加载完后就触发,load是在页面内包括dom结构,css,js,图片等都加载完成后再触发,显然ready更适合作为页面初始化使用。但有时候也不尽然。需要进一步查看其内部机制。
那么ready的内部是如何判断dom的结构加载完的?并且不同的浏览器的判断是如何的?
答案就在jquery代码内,假设jquery的版本是jquery-1.11.3.js。
ready的关键代码(3507~3566行),关键代码用红色标出:
jquery.ready.promise = function( obj ) { if ( !readylist ) { readylist = jquery.deferred(); // catch cases where $(document).ready() is called after the browser event has already occurred. // we once tried to use readystate interactive here, but it caused issues like the one // discovered by chriss here: http://bugs.jquery.com/ticket/12282#comment:15 if ( document.readystate === complete ) { // handle it asynchronously to allow scripts the opportunity to delay ready settimeout( jquery.ready ); // standards-based browsers support domcontentloaded } else if ( document.addeventlistener ) { // use the handy event callback document.addeventlistener( domcontentloaded, completed, false ); // a fallback to window.onload, that will always work window.addeventlistener( load, completed, false ); // if ie event model is used } else { // ensure firing before onload, maybe late but safe also for iframes document.attachevent( onreadystatechange, completed ); // a fallback to window.onload, that will always work window.attachevent( onload, completed ); // if ie and not a frame // continually check to see if the document is ready var top = false; try { top = window.frameelement == null && document.documentelement; } catch(e) {} if ( top && top.doscroll ) { (function doscrollcheck() { if ( !jquery.isready ) { try { // use the trick by diego perini // http://javascript.nwbox.com/iecontentloaded/ top.doscroll(left); } catch(e) { return settimeout( doscrollcheck, 50 ); } // detach all dom ready events detach(); // and execute any waiting functions jquery.ready(); } })(); } } } return readylist.promise( obj );};
上面的代码在触发ready时可以分成两部分
1.标准浏览器下的触发
当浏览器是基于标准浏览器时,会在加载完dom结构后触发“domcontentloaded”事件,jquery内部就用此事件作为ready的触发源。
document.addeventlistener( domcontentloaded, completed, false );
2.ie浏览器下的触发
当浏览器是ie浏览器时,因为ie浏览器(蛋疼并强大着)不支持“domcontentloaded”事件,所以只能另谋它法,
(function doscrollcheck() { if ( !jquery.isready ) { try { // use the trick by diego perini // http://javascript.nwbox.com/iecontentloaded/ top.doscroll(left); } catch(e) { return settimeout( doscrollcheck, 50 ); } // detach all dom ready events detach(); // and execute any waiting functions jquery.ready(); } })();
ie下的做法 就是上面代码的红字处,用“document.documentelement.doscroll(left)”的方法去滚动页面,如果没加载完就等个50毫秒后继续滚,直到滚的动后就触发ready。
但是,如果页面中有frame的场合,会使用window.onload事件作为ready的触发源。
所以在ie下,页面中有frame时,ready也是等到页面内的所有内容加载完成后再触发。
jquery中ready与load事件的区别
大家在工作中用jquery的时候一定会在使用之前这样:
//document ready$(document).ready(function(){ ...code...})//document ready 简写$(function(){ ...code...})
有些时候也会这么写:
//document load$(document).load(function(){ ...code...})
一个是ready一个是load,这两个到底有什么区别呢?今天我们来聊一聊。
ready与load谁先执行:
大家在面试的过程中,经常会被问到一个问题:ready与load那一个先执行,那一个后执行?答案是ready先执行,load后执行。
dom文档加载的步骤:
要想理解为什么ready先执行,load后执行就要先聊一下dom文档加载的步骤:
(1) 解析html结构。(2) 加载外部脚本和样式表文件。(3) 解析并执行脚本代码。(4) 构造html dom模型。//ready(5) 加载图片等外部文件。(6) 页面加载完毕。//load
从上面的描述中大家应该已经理解了吧,ready在第(4)步完成之后就执行了。但是load要在第(6)步完成之后才执行。
ready事件:
ready事件在dom结构绘制完成之后就绘执行。这样能确保就算有大量的媒体文件没加载出来,js代码一样可以执行。
load事件:
load事件必须等到网页中所有内容全部加载完毕之后才被执行。如果一个网页中有大量的图片的话,则就会出现这种情况:网页文档已经呈现出来,但由于网页数据还没有完全加载完毕,导致load事件不能够即时被触发。
总结:
相信大家已经了解了ready与load的区别,其实如果页面中要是没有图片之类的媒体文件的话ready与load是差不多的,但是页面中有文件就不一样了,所以还是推荐大家在工作中用ready。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product