JS实现img加载的图片不存在时加载默认图

来源:互联网 发布:大学软件工程学什么 编辑:程序博客网 时间:2024/06/06 18:31

实现原理很简单,加载图片时,如果图片不存在会触发error事件,那就捕获error事件来设置默认图片。


function (tag, dir, filePath, noPicture) {    $(this).one("error", function(e){        $(this).attr("src", noPicture);    });    $(this).attr('src', getFileUrl(tag, dir, filePath));};


0 0
原创粉丝点击