How to use jquery to handle picture bad chain specific implementation steps _jquery

Source: Internet
Author: User

Today I'm going to explain a little trick in the development of a page: How to deal with split graphs
In other words, what happens if this picture doesn't load successfully?
First step: HTML page:

Copy Code code as follows:

<! DOCTYPE html>
<title>gbin1</title>
<meta charset=utf-8/>
<script src= "Http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" >
</script>
<body>

</body>

Run this page and you'll see a bad picture.
The second part: is a JavaScript, its function is: if the picture is broken, a pop-up error prompts:
Copy Code code as follows:

$ (function () {
$ (' img '). Error (function () {
Alert (' Error ');
});
});

Run it, do you see it?
Part III: Next, we want to improve the friendly effect of the interface, we can call attr with a default picture to replace the split picture:
Copy Code code as follows:

$ (function () {
$ (' img '). Error (function () {
Alert (' Error ');
$ (this). attr (' src ', ' yun_qi_img/logohover.png ');
});
});

Have you seen the logo icon for the geek tag? This will enable the processing of the split graph.
Add a few: In fact, the error in jquery1.8 used to advocate, after 1.9 not too advocated, but still can use, personally feel quite convenient.
You can also change the following method: (Error replaced on, followed by a parameter)
Copy Code code as follows:

$ (function () {
$ (' img '). On (' Error ', function () {
Alert (' Error ');
$ (this). attr (' src ', ' yun_qi_img/logohover.png ');
});
});

In fact, the previous error method is a shortcut to the on method
By the way, if you test the error method locally, you need to start the server because the error method needs to check to see if the resource is available. For example, this test.html, you need to start the server, in the browser to enter http://localhost:8080/test.html before you can see the effect.
I hope we can help you, 3q!.

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.