Home  >  Article  >  Web Front-end  >  What to do if image loading fails in jquery

What to do if image loading fails in jquery

coldplay.xixi
coldplay.xixiOriginal
2020-11-27 14:38:322908browse

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地址)");
         });

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属性的意思是在图片为加载成功时显示的文字

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!

Statement:
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