If the image element encounters an error, replace it with a text:
Trigger error event for the image
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>//from ww w . j ava 2s . c o m <script> $(document).ready(function(){ $("img").error(function(){ $("img").replaceWith("<p>Error loading image!</p>"); }); $("button").click(function(){ $("img").error(); }); }); </script> </head> <body> <img src="image5.png" alt="image" width="200" height="200"><br> <button>test</button> </body> </html>
The error()
method was deprecated in jQuery version 1.8, and removed in version 3.0.
The error event occurs when an element encounters an error.
The error()
method triggers the error event.
The error()
method runs a function when an error event occurs.
The error()
method is a shortcut for bind('error', handler).
Trigger the error event for the selected elements:
$(selector).error()
Attach a function to the error event:
$(selector).error(function)
Parameter | Optional | Description |
---|---|---|
function | Optional. | the function to run when the error event occurs |