jquery: Uncaught TypeError: $(...).error 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42721804/
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
jquery: Uncaught TypeError: $(...).error is not a function
提问by user7432810
I am using jquery on my page and don't have problem with other parts. Even $.post and $.get are working fine.
我在我的页面上使用 jquery,其他部分没有问题。甚至 $.post 和 $.get 也能正常工作。
Now I am trying to evaluate if an image has any problems and tried this code:
现在我正在尝试评估图像是否有任何问题并尝试了以下代码:
$("#bib").error(function(){
//nothing now
});
I am wondering why I get this error:
我想知道为什么我会收到这个错误:
Uncaught TypeError: $(...).error is not a function
at app.js:53
(anonymous) @ app.js:53
As you see I have shortened the code to isolate the problem but you get the whole idea. At the moment these jquery sources are included inside the page:
如您所见,我缩短了代码以隔离问题,但您已经了解了整个想法。目前这些 jquery 源包含在页面中:
https://code.jquery.com/jquery-3.1.0.min.jshttps://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js
https://code.jquery.com/jquery-3.1.0.min.js https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js
Any idea why this happens?
知道为什么会这样吗?
回答by Neil
Use this instead (as stated earlier, .error has been removed).
改用它(如前所述,.error 已被删除)。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$("#bib").on('error', function(){
//nothing now
});
</script>
回答by freedomn-m
From the jquery upgrade guide at: https://jquery.com/upgrade-guide/3.0/#breaking-change-load-unload-and-error-removed
来自 jquery 升级指南:https: //jquery.com/upgrade-guide/3.0/#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 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)
.
重大更改: .load()、.unload() 和 .error() 已删除
这些方法是事件操作的快捷方式,但有几个 API 限制。事件
.load()
方法与ajax.load()
方法冲突。由于 DOM 方法的定义方式,该.error()
方法无法使用window.onerror
。如果您需要通过这些名称附加事件,请使用该.on()
方法,例如更改$("img").load(fn)
为$("img").on("load", fn)
.