Home > Article > Web Front-end > What to do if image loading fails in jquery
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.
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='test.jpg' alt='test' onerror="this.src='default.jpg'"> //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!