【发布时间】:2014-08-22 02:22:39
【问题描述】:
我有一个网站链接到其他网站上的不同图片。有时这些图像已被删除或域不再存在等。为了不显示这些图像并删除它们,我使用 jQuery 来执行此操作:
// catch image load errors
$( "img" ).error(function() {
// read out image source:
var src = $(this).attr('src');
// post the source to the server for the image to be removed
$.post("/pajax/image/notLoaded/", {url:src},
function success(obj) {
if(obj.status =='ok'){
console.log('removed: '+src);
}
},
"json" );
// hide the image & its container div
$( this ).hide();
$( this ).closest(".item-img").hide();
这适用于不再存在且未发送数据的图像。但是,通常会发送足够多的 HTML 页面,在这种情况下 jQuery 不会抛出错误,但控制台会显示“资源解释为图像但使用 MIME 类型文本/html 传输:”
有没有办法用 jQuery 来捕捉它?
【问题讨论】:
标签: javascript jquery html image