jQuery - $(document).ready 和 $(window).load 之间有什么区别? [英] jQuery - What are differences between $(document).ready and $(window).load?
问题描述
有什么区别
$(document).ready(function(){
//my code here
});
和
$(window).load(function(){
//my code here
});
<小时>
我想确保:
And I want to make sure that:
$(document).ready(function(){
})
和
$(function(){
});
和
jQuery(document).ready(function(){
});
都一样.
你能告诉我它们之间有什么区别和相似之处吗?
Can you tell me what differences and similarities between them?
推荐答案
$(document).ready(function() {
// executes when HTML-Document is loaded and DOM is ready
console.log("document is ready");
});
$(window).load(function() {
// executes when complete page is fully loaded, including all frames, objects and images
console.log("window is loaded");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
查询 3.0 版本
这些方法是事件操作的快捷方式,但是有几个API限制.事件 .load()
方法与 ajax .load()
冲突方法..error()
方法不能与 window.onerror
一起使用因为 DOM 方法的定义方式.如果您需要附加这些名称的事件,使用 .on()
方法,例如改变$("img").load(fn)
到 $(img).on("load", fn)
.1
These methods are shortcuts for event operations, but had several API limitations. The event
.load()
method conflicted with the ajax.load()
method. The.error()
method could not be used withwindow.onerror
because of the way the DOM method is defined. If you need to attach events by these names, use the.on()
method, e.g. change$("img").load(fn)
to$(img).on("load", fn)
.1
$(window).load(function() {});
应该改为
$(window).on('load', function (e) {})
这些都是等价的:
$(function(){
});
jQuery(document).ready(function(){
});
$(document).ready(function(){
});
$(document).on('ready', function(){
})
这篇关于jQuery - $(document).ready 和 $(window).load 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!