存在JQuery检查图像 [英] JQuery check image exist
本文介绍了存在JQuery检查图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我正在尝试在我的网站的不同部分显示不同的图像,但如果图像不存在,我需要显示默认图像。
Hi i'm trying to display different image in different part of my site, but i need to display a default image if image doesn't exist.
此处是我的代码:问题是检查img宽度但总是
here is my code: the problem is to check the img width but is alway
jQuery(document).ready(function () {
var $pathname = window.location.pathname;
$item = 'http://www.lotus-nascita.it/images/headers-hover/corsi-preparto.jpg';
if ($pathname == '/') {
$pathname = $pathname + 'home';
$item = 'http://www.lotus-nascita.it/images/headers-hover' + $pathname + '.jpg';
}
var img = new Image();
img.onload = function () {}
img.src = $item;
if (img.height != 0) {
$item = 'http://www.lotus-nascita.it/images/headers-hover' + $pathname + '.jpg';
}
jQuery('#header').fadeOut('fast', function () {
jQuery('#header').css({
"background": 'url("' + $item + '")'
});
jQuery('#header').fadeIn('slow');
});
});
推荐答案
高度始终为零,因为你必须等待图像在高度之前加载?
The height is always zero, as you have to wait for the image to load before it has a height ?
var item = 'http://www.lotus-nascita.it/images/headers-hover' + $pathname + '.jpg';
var img = new Image();
img.onload = function () {
if (this.height === 0) {
item = 'http://www.lotus-nascita.it/images/headers-hover/corsi-preparto.jpg';
}
}
img.src = item;
这篇关于存在JQuery检查图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文