JS Picture loading Failure Processing method

Source: Internet
Author: User

In the project will inevitably use the picture, especially the list, sometimes the picture will fail to load, it will show a hard to see the bad picture thumbnail, the following two methods to solve the problem:

1. If you have introduced a jquery plugin in your project, you can use the error ([[DATA],FN]) function;

$ ("img"). Error (function() {//) the action you want to take when the picture fails to load
$ (this). attr (' src ', ' images/no_pic.jpg ');});

2, if the project does not have a plug-in such as jquery, you can use the HTML DOM event onerror event processing;

in HTML: <element onerror= "MyScript" > Try It
In JavaScript: object.onerror=function() {myScript}; try it.
JavaScript, use the AddEventListener () method: (Note: Internet Explorer 8 and earlier versions of IE do not support the AddEventListener () method.) ) Object.addeventlistener ("error", MyScript);

Supported HTML Tags: , <input type= "image";, <object>, <script>, <style>

JS Picture loading Failure Processing method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.