我在这里找到了前面提到的这个问题,jQuery/Javascript to replace broken images
这是最好的答案..我试过了,它工作得很好
function ImgError(source){
source.src = "/images/noimage.gif";
source.onerror = "";
return true;
}
<img src="someimage.png" onerror="ImgError(this);"/>但是当我尝试在前面的函数中执行其他事件时
function ImgError(source){
source.hide(); // hide instead of replacing the image
source.onerror = "";
return true;
}我得到了这个错误
TypeError: 'undefined' is not a function (evaluating 'source.hide()')我尝试该函数的每一个jQuery事件/方法都会得到一个TypeError。
有什么问题吗?
发布于 2012-07-26 06:02:27
因为.hide()是一个jQuery元素。
试一试:
function ImgError(source) {
$(source).hide();
source.onerror = "";
return true;
}发布于 2012-07-26 06:04:30
hide()是一种jQuery方法,对吧?
试试$(source).hide()
:)
编辑:您可以尝试为所有图像添加一个全局错误处理程序,如下所示:
$('img').error(function() { $(this).hide(); });
请参阅:http://api.jquery.com/error/
发布于 2012-07-26 06:05:07
您是否尝试过以下操作:
source.style.visibility='hidden';https://stackoverflow.com/questions/11659292
复制相似问题