$( document ).ready()&$(window).load()
$( document ).ready()
https://learn.jquery.com/using-jquery-core/document-ready/
A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ).ready()
will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute. Code included inside $( window ).load(function() { ... })
will run once the entire page (images or iframes), not just the DOM, is ready.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>$( document ).ready()</title>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function () {
alert("document loaded");
});
//document ready 简写
$(function() {
// ...代码...
})
$(window).load(function () { alert("window loaded"); }); </script> </head> <body> <iframe src="http://techcrunch.com"></iframe> </body> </html>
Experienced developers sometimes use the shorthand $()
for $( document ).ready()
. If you are writing code that people who aren't experienced with jQuery may see, it's best to use the long form.
1
2
3
4
|
// Shorthand for $( document ).ready() $(function() { console.log( "ready!" ); }); |
You can also pass a named function to $( document ).ready()
instead of passing an anonymous function.
1
2
3
4
5
6
7
8
9
|
// Passing a named function instead of an anonymous function. function readyFn( jQuery ) { // Code to run when the document is ready. } $( document ).ready( readyFn ); // or: $( window ).load( readyFn ); |
DOMContentLoaded
https://developer.mozilla.org/zh-CN/docs/DOM/DOM_event_reference/DOMContentLoaded
当页面中的文档树解析完成时,在页面的Document对象上,会触发DOMContentLoaded
事件.该事件代表了,页面的DOM树已经构建完成,但页面引用的样式表和图像文件,以及包含的框架页面可能还没有加载完成,在页面完全加载完成时,会触发另外一个类似的称为"load"的事件.
该事件会冒泡到当前页面的window
对象上.但框架页面中文档加载完成时并不会触发父页面的DOMContentLoaded事件.
浏览器兼容性
随机推荐
- yii2.0 url 跳转
//转发 $this->render('page1',['id'=>3,'mark'=>2]); //显示page1页面 并传递 id mark 2个参数 //重定向 $thi ...
- 点击li,点击的li添加class,其余去掉class
点击li,点击的li添加class,其余去掉class <script type="text/javascript"> $(function () { var liob ...
- android.os.NetworkOnMainThreadException解决
很早就知道Android4.0以后,要把耗时的网络操作放在多线程中,处理方法有: 1) setContentView(R.layout.activity_main)下面加上如下代码 if (andro ...
- PHP中超全局变量$GLOBALS和global的区别
一.超全局变量$GLOBALS PHP超全局变量有很多,如下的都属于超全局变量(Superglobal): $GLOBALS,$_SERVER,$_GET,$_POST,$_FILES,$_COOKI ...
- hdu 1115(计算多边形重心)
题意:已知一多边形没有边相交,质量分布均匀.顺序给出多边形的顶点坐标,求其重心. 分析: 求多边形重心的题目大致有这么几种: 1,质量集中在顶点上.n个顶点坐标为(xi,yi),质量为mi,则重心 X ...
- vector.resize 与 vector.reserve的区别 .xml
pre{ line-height:1; color:#9f1d66; background-color:#a0ffc0; font-size:16px;}.sysFunc{color:#5d57ff; ...
- java 创建线程
一.继承Thread类 为创建一个线程,最简单的方法就是从Thread类继承.这个类包含了创建和运行线程所需的一切东西.Thread类最重要的方法是run(),但为了使用run(),必须对其进行重写. ...
- backbone-1.3.3源码解析-----------Event
第一次写,写的不对的请指正 backbone.js中的Event实现了自定义事件.自定义事件就是一个对象的键值对,key为事件名,value为一个function数组.在backbone这个对象中有一 ...
- C++ 容器一些细节
今天学习是看到了讲解C++容器的一些细节用法,故记之!参考:http://www.cnblogs.com/answeryi/archive/2011/12/16/2289811.html: 目录 == ...
- 一起来画画!8款最佳HTML5绘图工具
HTML5无疑是当前最受宠的一项技术,今天推荐8款HTML5绘图工具,同样惊艳你的眼球!这些绘图工具大多数是用HTML5画布(Canvas)实现的,部分辅以Javascript.对每一个web设计者来 ...