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.
有人知道如何检查图像是否正确加载。我有一个预加载图像的脚本,我想做的是如果图像不存在,这会导致我的脚本不结束,我只需将属性“src”设为默认值
image.load(function(){ //it never reache here if the image doesn't exist }).attr('src','a non existing image');
谢谢
var image = $('<img>').appendTo('body'); image.load(function() { // image is found and loaded }).error(function() { // image not loaded image.attr('src','default-image.jpg'); }).attr('src','a non existing image');