返回值:jQueryerror(handler(eventObject))
Bind an event handler to the "error" JavaScript event.
-
1.0 新增error(handler(eventObject))
handler(eventObject) (Function) A function to execute when the event is triggered. -
1.4.3 新增error([eventData], handler(eventObject))
eventData (Object) 可选参数,将要传递给事件处理函数的数据映射。handler(eventObject) (Function) 每当事件触发时执行的函数。
This method is a shortcut for .bind('error', handler)
.
The error
event is sent to elements, such as images, that are referenced by a document and loaded by the browser. It is called if the
element was not loaded correctly.
For example, consider a page with a simple image:
<img src="missing.png" alt="Book" id="book" />
The event handler can be bound to the image:
$('#book').error(function() { alert('Handler for .error() called.') });
If the image cannot be loaded (for example, because it is not present at the supplied URL), the alert is displayed:
Handler for .error() called.
This event may not be correctly fired when the page is served locally. Since
error
relies on normal HTTP status codes, it will generally not be triggered if the URL uses thefile:
protocol.
Note: A jQuery error event handler should not be attached to the window object. The browser fires the window's error event when a script error occurs. However, the window error event receives different arguments and has different return value requirements than conventional event handlers.
示例:
To hide JavaScript errors from the user, you can try:
jQuery 代码:
$(window).error(function(){
return true;
});
示例:
To hide the "broken image" icons for your IE users, you can try:
jQuery 代码:
$("img").error(function(){
$(this).hide();
});