jQuery error()Method

jQuery 事件方法jQuery Event Methods

Example

If the image element encounters an error, replace it with text:

$("img").error(function(){
$("img").replaceWith("<p>Image loading error!</p>");
});

Try it Yourself »

Definition and Usage

The error() method was deprecated in jQuery version 1.8(deprecated)。

When an element encounters an error (when the element is not loaded correctly), the error event occurs.

The error() method triggers the error event, or specifies a function to run when the error event occurs.

Tip:This method is a shorthand for bind('error', handler).


Syntax

Trigger the error event for the selected element:

$(selector).error() Try it Yourself

Add a function to the error event:

$(selector).error(function) Try it Yourself

Parameter Description
function Optional. Specifies the function to run when the error event occurs.


jQuery 事件方法jQuery Event Methods

Other Extensions