Home > Web Front-end > JS Tutorial > body text

What to do if image loading fails in jquery

coldplay.xixi
Release: 2020-12-02 17:25:19
Original
3069 people have browsed it

Solution to the failure of image loading in jquery: 1. Introduce the jquery plug-in, the code is [$(this).attr('src', "default.jpg (default image url address)")]; 2. Use the onerror event of img.

What to do if image loading fails in jquery

The operating environment of this tutorial: Windows 7 system, jquery version 3.2.1. This method is suitable for all brands of computers.

Solution to image loading failure in jquery:

1. The first method:

If it has been introduced The jquery plug-in is easy to handle. If not, if you really need it, you can attach the code:

    script(type='text/javascript', src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js")
    //这是jade文件的写法,可以自行转换为html
handle error
    $('img').error(function(){
            $(this).attr('src', "default.jpg(默认图片的url地址)");
         });
Copy after login

2. The second method: If the img tag is a small amount, you can use this:

img onerror event

<img src=&#39;test.jpg&#39; alt=&#39;test&#39; onerror="this.src=&#39;default.jpg&#39;">
//alt属性的意思是在图片为加载成功时显示的文字
Copy after login

Related free learning recommendations: javascript (video)

The above is the detailed content of What to do if image loading fails in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template