且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在将图像添加为背景图像之前检查图像是否存在

更新时间:2022-06-01 07:13:11

有几种方法可以做到这一点:

There is a few ways to do that:

使用ajax:

$.ajax({
    url: "image.jpg",
    type: "HEAD",
    error: function () { alert("no image"); }
});

使用javascript图像对象:

Using the javascript image object:

var image = new Image(); 
image.src = "image.jpg";
if (image.width == 0) {
  alert("no image");
}