$( document ).ready()
2015-11-23 13:10
591 查看
Apagecan'tbemanipulatedsafelyuntilthedocumentis"ready."jQuerydetectsthisstateofreadinessforyou.Codeincludedinside
Experienceddeveloperssometimesusetheshorthand
Youcanalsopassanamedfunctionto
function.
Theexamplebelowshows
checksforbothevents:
https://learn.jquery.com/using-jquery-core/document-ready/
$( document).ready()willonlyrunoncethepageDocumentObjectModel(DOM)isreadyforJavaScriptcodetoexecute.Codeincludedinside
$( window).load(function(){...})willrunoncetheentirepage(imagesoriframes),notjusttheDOM,isready.
1 2 3 4 | //A$(document).ready()block. $(document).ready(function(){ console.log("ready!"); }); |
$()for
$( document).ready().Ifyouarewritingcodethatpeoplewhoaren'texperiencedwithjQuerymaysee,it'sbesttousethelongform.
1 2 3 4 | //Shorthandfor$(document).ready() $(function(){ console.log("ready!"); }); |
$(document).ready()insteadofpassingananonymous
function.
1 2 3 4 5 6 7 8 9 | //Passinganamedfunctioninsteadofananonymousfunction. functionreadyFn(jQuery){ //Codetorunwhenthedocumentisready. } $(document).ready(readyFn); //or: $(window).load(readyFn); |
$(document).ready()and
$(inaction.ThecodetriestoloadawebsiteURLinan
window).load()
<iframe>and
checksforbothevents:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <html> <head> <scriptsrc="https://code.jquery.com/jquery-1.9.1.min.js"></script> <script> $(document).ready(function(){ console.log("documentloaded"); }); $(window).load(function(){ console.log("windowloaded"); }); </script> </head> <body> <iframesrc="http://techcrunch.com"></iframe> </body> </html> |
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- JavaScript演示排序算法
- 2015-2016网页设计趋势分析 Web Design of Trends
- jQuery Ajax 跨域调用
- 移动端的长按事件
- jquery教程靠边站,一分钱不花让你免费学会jquery
- JQuery+Strusts1.x无刷新登录
- JavaScript 各种遍历方式详解
- jQuery菜单插件用法实例
- JQuery 初体验(建议学习jquery)
- Jquery实现的table最后一行添加样式的代码
- jQuery实现向下滑出的平滑下拉菜单效果
- jQuery 练习[一] 学习jquery的准备工作
- jquery获得页面元素的坐标值实现思路及代码
- 使用Browserify配合jQuery进行编程的超级指南
- jquery如何实现在加载完iframe的内容后再进行操作
- jquery $.ajax()取xml数据的小问题解决方法
- jQuery '行 4954 错误: 不支持该属性或方法' 的问题解决方法
- Jquery 表单取值赋值的一些基本操作
- jQuery实现复选框批量选择与反选的方法