gpt4 book ai didi

jQuery - $(document).ready 和 $(window).load 之间有什么区别?

转载 作者:IT王子 更新时间:2023-10-29 03:23:38 25 4
gpt4 key购买 nike

有什么区别

$(document).ready(function(){
//my code here
});

$(window).load(function(){
//my code here
});

我想确保:

$(document).ready(function(){

})

$(function(){

});

jQuery(document).ready(function(){

});

是一样的。

你能告诉我它们之间有什么异同吗?

最佳答案

$(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版本

Breaking change: .load(), .unload(), and .error() removed

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 之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8396407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com