Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有没有办法使用 jQuery 检查图像是否存在?
我认为以下内容会起作用,但它不起作用,并且如果图像不存在,console.log 会显示图像的错误 404。
$("#icon").html("<img src='/assets/img/"+condition+".png'>"); if (!$("#icon")){ $("#icon").html("nothing to show"); }
试试这个 :
$("#icon").html("<img src='/assets/img/"+condition+".png'>"); $("#icon img").error(function() { $("#icon").html("nothing to show"); });
</p>