1

我相信你已经多次看到这个问题,但我向你保证这是不同的。我想知道如何使用带有 url 变量的图像路径来检查图像是否存在。我想要这样的东西:

if ($('url').length ) {
    $(".mainContent").html("File exists");
} 
else
    $(".mainContent").html("File does not exists");`

希望你能尽快帮助我...

4

1 回答 1

0

对于可能存在的图像,我发现最优雅的解决方案是使用 $ajax,例如:

$.ajax({
    url: 'your_image.jpg',
    type: "POST",
    dataType: "image",
    success: function() {
        /* function if image exists (setting it in div or smthg.)*/
    },
    error: function(){
       /* function if image doesn't exist like hideing div or setting default image*/


      } 

})

但只是要警告有些人坚持使用 GET 而不是 POST 但我没有任何问题,它就像一个魅力

于 2013-08-14T13:35:47.210 回答