1

我发现这里之前提到的这个问题jQuery/Javascript 来替换损坏的图像

这是最好的答案..我试过了,它工作正常

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。

有什么问题?

4

3 回答 3

1

因为.hide()是一个jQuery元素。

试试看:

function ImgError(source) {
   $(source).hide();
   source.onerror = "";
   return true;
}
于 2012-07-25T22:02:26.800 回答
0

hide()是一个jQuery方法,是吗?

尝试$(source).hide()

:)

编辑:您可以尝试为所有图像添加全局错误处理程序,如下所示:

$('img').error(function() { $(this).hide(); });

见: http ://api.jquery.com/error/

于 2012-07-25T22:04:29.853 回答
0

您是否尝试过以下操作:

source.style.visibility='hidden';
于 2012-07-25T22:05:07.427 回答