jQuery .ready()
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.
-
version added: 1.0.ready( handler )
-
handler
Type: Function()
A function to execute after the DOM is ready.
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.
});
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2016-06-27 通过反射,获取类的属性
2015-06-27 Svn忽略配置
2014-06-27 设计模式网页资料