jQuery 检测到损坏的图像太晚了

标签 jquery html http-status-code-404

我正在尝试检测 jQuery 中损坏的图像,在这种情况下,我想删除它们并用它的替代文本替换。 我正在使用我在 SO 上找到的以下代码;

$(document).ready(function(){
    $('img.logoimg').error(function() {
        $(this).unbind("error");
        $(this).parent().prepend("<strong>"+$(this).attr("alt")+"</strong>");
        $(this).remove();
    });
    //...
}

这在 FF 中工作正常。

然而,在 Chrome 中,浏览器仍然显示一个正方形,表示图像损坏。 似乎 error 事件触发得太晚,无法检测到损坏的图像。

我可以让它早点开火吗?代码还有什么问题吗?

[编辑] 在下面添加了一个解决方案。任何能够解释这一点的人都可以获得这个问题的答案。

最佳答案

我已经通过消除确定了问题。这不是你想的地方。 在我的脚本标签下,我有一段 Google Ads 代码:

<script type="text/javascript" src="my_javascript.js"></script>
<script type='text/javascript' src='http://partner.googleadservices.com/gampad/google_service.js'></script>
<script type='text/javascript'>GS_googleAddAdSenseService("ca-pub-XXXXXXXXXXXXXX");GS_googleEnableAllServices();</script>
<script type="text/javascript">
    GA_googleAddSlot("ca-pub-XXXXXXXXXXXXXX","banner_1");
    GA_googleAddSlot("ca-pub-XXXXXXXXXXXXXX","banner_2");
    GA_googleAddSlot("ca-pub-XXXXXXXXXXXXXX","banner_3");

    GA_googleFetchAds();
</script>

这是最后一次调用:GA_googleFetchAds() 终止了它。删除该电话似乎可以解决我的问题。奇怪的是。

我现在已经这样解决了:

$(document).ready(function(){
    GA_googleFetchAds();
});

任何能给出合理解释的人都将赢得我永恒的尊重和感激。

关于jQuery 检测到损坏的图像太晚了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6580031/

相关文章:

javascript - 你能修复一个 div 并让它在你使用 CSS3 滚动时改变不透明度、大小和位置吗

ruby-on-rails - 当我尝试使用 https 访问我的 Rails 站点时,为什么会出现 404 错误?

python - Flask 应用程序在 Xampp 中返回 404

javascript - ctx.fillText 不绘制连接

javascript - Whatsapp:无法与 Javascript 共享当前链接

pythonanywhere 404错误

javascript - 我怎样才能将图像放入选择中?

javascript - Wordpress:根据 get_post_custom() 字段更改样式

javascript - 如何将div追加到现有div中?

html - 使用 CSS,当我将鼠标悬停在一个 div 标签上时,我可以将背景更改为多个 div 标签吗?