https://www.w3schools.com/jquery/event_ready.asp

Example

Use ready() to make a function available after the document is loaded:

$(document).ready(function(){
  $("button").click(function(){
    $("p").slideToggle();
  });
});

Definition and Usage

The ready event occurs when the DOM (document object model) has been loaded.

Because this event occurs after the document is ready, it is a good place to have all other jQuery events and functions. Like in the example above.

The ready() method specifies what happens when a ready event occurs.

Tip: The ready() method should not be used together with <body onload="">.

只能在document上使用

 

.ready()

.ready( handler )Returns: jQuery

Description: Specify a function to execute when the DOM is fully loaded.

The .ready() method offers a way to run JavaScript code as soon as the page's Document Object Model (DOM) becomes safe to manipulate.

This will often be a good time to perform tasks that are needed before the user views or interacts with the page, for example to add event handlers and initialize plugins.

When multiple functions are added via successive calls to this method, they run when the DOM is ready in the order in which they are added.

As of jQuery 3.0, jQuery ensures that an exception occuring in one handler does not prevent subsequently added handlers from executing.

Most browsers provide similar functionality in the form of a DOMContentLoaded event.

However, jQuery's .ready() method differs in an important and useful way:

If the DOM becomes ready and the browser fires DOMContentLoaded before the code calls .ready( handler ), the function handler will still be executed.

In contrast, a DOMContentLoaded event listener added after the event fires is never executed.

Browsers also provide the load event on the window object.

When this event fires it indicates that all assets on the page have loaded, including images.

This event can be watched in jQuery using $( window ).on( "load", handler ).

In cases where code relies on loaded assets (for example, if the dimensions of an image are required), the code should be placed in a handler for the load event instead.

Note that although the DOM always becomes ready before the page is fully loaded, it is usually not safe to attach a load event listener in code executed during a .ready() handler.

For example, scripts can be loaded dynamically long after the page has loaded using methods such as $.getScript().

Although handlers added by .ready() will always be executed in a dynamically loaded script, the window's load event has already occurred and those listeners will never run.

jQuery offers several ways to attach a function that will run when the DOM is ready. All of the following syntaxes are equivalent:

  • $( handler )
  • $( document ).ready( handler )
  • $( "document" ).ready( handler )
  • $( "img" ).ready( handler )
  • $().ready( handler )

As of jQuery 3.0, only the first syntax is recommended; the other syntaxes still work but are deprecated. This is because the selection has no bearing on the behavior of the .ready() method, which is inefficient and can lead to incorrect assumptions about the method's behavior. For example, the third syntax works with "document" which selects nothing. The fourth syntax waits for the document to be ready but implies (incorrectly) that it waits for images to become ready.

There is also $(document).on( "ready", handler ), deprecated as of jQuery 1.8 and removed in jQuery 3.0. Note that if the DOM becomes ready before this event is attached, the handler will not be executed.

The .ready() method is typically used with an anonymous function:

$( document ).ready(function() {
// Handler for .ready() called.
});

Which is equivalent to the recommended way of calling:

$(function() {
// Handler for .ready() called.
});

最新文章

  1. 基于Ruby的Watir-WebDriver自动化测试框架
  2. Requests库练习
  3. 我的c++学习(12)指针作为函数参数
  4. Android:自定义Dialog大小,显示圆角
  5. Linux--线程安全与可重入函数的异同
  6. Java进阶(四十一)多线程讲解
  7. Asp.Net Core微服务初体验
  8. yum方式安装kubernetes
  9. 统计分析与R软件-chapter2-5
  10. spring AOP自定义注解方式实现日志管理
  11. 安卓开发笔记——个性化TextView(新浪微博)
  12. HPU第四次积分赛-K :方框(水题,打印图形)
  13. 附1 Java内存模型与共享变量可见性
  14. HDU 4706 Children&#39;s Day (水题)
  15. SVN如何查看修改的文件记录
  16. 第10章 vim程序编辑器
  17. Ros学习——Movebase源码解读
  18. shell基础笔记
  19. dxjk服务器安装 lamp
  20. Socket.Receive 无法预知字节长度的数据接收

热门文章

  1. Hive调优参数配置
  2. Java 线程池 8 大拒绝策略,面试必问!
  3. Java语言的发展历程
  4. C++中构造函数的手动和自动调用方式
  5. python文件打包/导入 .so 文件
  6. Django中Model进阶操作
  7. HDU-1018 BigNumber
  8. Chrome开发者工具详解(四)之Elements、Console、Sources面板
  9. 关于使用iframe的父子页面进行简单的相互传值
  10. background的水平条纹和斜向条纹