我正在尝试检测 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/