且构网

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

jQuery:检查图像是否存在

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

好了,可以绑定 .error() 处理程序...

Well, you can bind .error() handler...

更新:在 jQuery 3.0 及更高版本中:

Update: In jQuery 3.0 and later:

$(".myimage").on("error", function() {
    $(this).hide();
});

注意:.error() 在 jQuery 1.8 中已弃用并在 3.0 中删除,但在旧版本中,您可以:

note: .error() was deprecated in jQuery 1.8 and removed in 3.0 but in in older versions you can:

$(".myimage").error(function(){
  $(this).hide();
});

好吧,你的已经可以使用 load-event

well, yours is okay already with load-event

$(".myimage").load(function() {
    $(this).show();
});

这里的问题是如果 Javascript 被禁用,图像将永远不会显示...

the problem with this is if Javascript was disabled the image will not ever show...