jQuery 3.0 $(window).load(function(){});

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37751179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:07:59  来源:igfitidea点击:

jQuery 3.0 $(window).load(function(){});

jquery

提问by Matt Cowley

so jQuery 3.0 was released today, and for some reason the following code no longer works on my site:

所以今天发布了 jQuery 3.0,由于某种原因,以下代码不再适用于我的网站:

$(window).load(function() {});

Can anyone suggest how I fix this or an alternative that actives when /everything/ is loaded?

任何人都可以建议我如何解决这个问题或在加载 /everything/ 时激活的替代方法吗?

回答by gaetanoM

Reading from breaking-change-load-unload-and-error-removed:

break-change-load-unload-and-error-removed读取:

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).

重大更改: .load()、.unload() 和 .error() 已删除

这些方法是事件操作的快捷方式,但有几个 API 限制。事件 .load() 方法与 ajax .load() 方法冲突。由于 DOM 方法的定义方式,.error() 方法不能与 window.onerror 一起使用。如果您需要通过这些名称附加事件,请使用 .on() 方法,例如将 $("img").load(fn) 更改为 $(img).on("load", fn)。

Therefore, you need to change:

因此,您需要更改:

$(window).load(function() {});

to:

到:

$(window).on("load", function (e) {})