$( 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事件.
浏览器兼容性
随机推荐
- Android下Fragment的动画切换效果
效果图如下: 源码链接 : 请戳这里
- 脚本乐园 Shell中命令行选项和参数的处理
在Linux的Shell中怎样处理tail -n 10 access.log这样的命令行选项呢?这是被别人问起的一个问题,好好学习了一下,进行总结如下:在bash中,可以用以下三种方式来处理命令行参数 ...
- 关于浮动-float
1.存在浏览器兼容问题:js代码 2.对于这种存在浏览器兼容问题的问题,我们可以绕开兼容性问题,先在css样式写好,然后通过该变className 3.学习的博客 https://paran.io/c ...
- 设置TextView控件的背景透明度和字体透明度
TextView tv = (TextView) findViewById(R.id.xx); 第1种:tv.setBackgroundColor(Color.argb(255, 0, 255, 0) ...
- 性能测试之LoardRunner 测试场景监控关注的几点
1.系统业务处理能力,即通常我们在进行性能测试的时候,在特定的硬件和软件环境下考察的业务处理能力,即“事物”,需要关注当前.平时.峰值以及长远未来业务发展情况,考虑不同业务的处理数量,从而设定相应的业 ...
- NGUI-制作位图字体以及图文混排
制作字体过程 首先得下载一个位图制作工具Bitmap font generator,可以点击这里下载 1.新建txt文件,输入字体里面包含的文字 2.保存为utf-8格式:点击文件另存为,选择编码格式 ...
- java学习随笔--- 捣蛋vector
最近比较有时间啦,有时间搞下java,个人觉得学这门语言语法太多啦,不一一去学习啦,心血来潮,挂了个struct2的源代码,一入深似海啊,看得我天花缭乱,从最简单的开始吧 public static ...
- MVC&&MVP
Classic MVC Classic MVC 大概上世纪七十年代,Xerox PARC的Trygve提出了MVC的概念. 并应用在Smalltalk系统中,为了和其它类型的MVC加以区分,历史上习惯 ...
- 隐藏apache版本号 PHP版本号
httpd-default.conf ServerTokens Prod ServerSignature Off php.ini expose_php Off 重启服务器
- 记录一下JS正则的坑
JS正则的单行模式有点问题 总之 . 符号匹配换行符号会有问题 暂时的解决方案是 html = html.replace(/[\r\n]/g,""); 附加一个 html = ht ...