2012-07-25 68 views
1

我发现以前这里jQuery/Javascript to replace broken imagesjQuery的执行,当图像被打破

提到了这个问题,这是最好的答案的事件。我尝试过了,它的正常工作

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。

有什么问题?

+2

您没有使用jQuery。 – SLaks 2012-07-25 22:05:14

回答

1

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

试一下:

function ImgError(source) { 
    $(source).hide(); 
    source.onerror = ""; 
    return true; 
} 
0

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

尝试$(source).hide()

:)

编辑: 您可以尝试添加一个全局错误处理程序,像这样所有图片:

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

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

+0

感谢格式化的穆萨! – nebulae 2012-07-25 22:07:58

0

做您尝试了以下操作:

source.style.visibility='hidden';