jQuery-$(document).ready和$(window).load有什么区别? [英] jQuery - What are differences between $(document).ready and $(window).load?

查看:100
本文介绍了jQuery-$(document).ready和$(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 with window.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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆