jQuery 不推荐使用 load() 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12643160/
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
load() method deprecated?
提问by Johan
I was browsing through the jQuery api and noticed that the load methodis on the deprecated list.
我正在浏览 jQuery api 并注意到load 方法在弃用列表中。
Categories: Deprecated | Events > Document Loading
类别:已弃用 | 事件 > 文档加载
I usually use this method to check if images are completly loaded. Why is it deprecated? And what am I supposed to be using instead?
我通常使用这种方法来检查图像是否已完全加载。为什么它被弃用?我应该用什么代替?
回答by Frédéric Hamidi
See bug #11733, which documents this deprecation:
请参阅错误 #11733,其中记录了此弃用:
The
.load()
method is an ambiguous signature, it can either be an ajax load or attach/fire a "load" event. CCAOcannot tell them apart since it's a dynamic decision based on arguments.
该
.load()
方法是一个不明确的签名,它可以是 ajax 加载或附加/触发“加载”事件。CCAO无法区分它们,因为这是基于争论的动态决定。
To avoid ambiguities related to the method's signature, it is now recommended to use on()instead. For instance:
为避免与方法签名相关的歧义,现在建议改用on()。例如:
$("selector").load(function() {
// ...
});
Should become:
应该变成:
$("selector").on("load", function() {
// ...
});
回答by Mohsin Shoukat
load function deprcated in jQuery alternative of it is on which you can use like
jQuery 中已弃用的加载函数替代它,您可以在其上使用
$("iframe").on("load",function()
{
alert("on loaded iframe");
});
this work perfect for jquery-3.1.1.
这项工作非常适合 jquery-3.1.1。
回答by Paul Sweatte
If load
does not work as expected, an alternative is:
如果load
没有按预期工作,另一种方法是:
$(window).one("scroll", foo);
Or
或者
$(window).one("scroll", function(){/*...*/});
Specifically, scroll event binding is useful in Android when DOMContentLoaded
doesn't work as expected, and IE8 and below when onreadystatechange
does not work as expected.
具体来说,滚动事件绑定在 AndroidDOMContentLoaded
中未按预期工作时很有用,而 IE8 及以下版本在onreadystatechange
未按预期工作时很有用。
References
参考