函数中的jQuery img错误管理



当在客户端找不到映像时,我正在尝试在服务器端记录错误。为此,我想调用一个ajax函数来记录服务器端的错误。

我试着在img标签上添加oneror,但我不能在oneror上调用函数(它很好——我只是在onerorr本身上进行错误管理,而不调用其他函数)。

加载图像时出错,如何调用函数?

正如您在这个小提琴中看到的,只显示hello 1和hello 2。

函数中的hello3没有显示(我有以下错误"Uncaught ReferenceError:hello3没有定义")

hello 4也没有显示,似乎错误函数已弃用。

http://jsfiddle.net/Ljj68/3/

HTML代码:

<script>
    function hello2() {
        alert('hello 2');
    }
</script>
<img src='test1.png' onerror='alert("hello 1")'>
<img src='test2.png' onerror='hello2()'>
<img src='test2.png' onerror='hello3()'>
<img id'img_4' src='test2.png'>

Javascript代码:

function hello3() {
    alert('hello 3');
}
$('#img_4').error(function() {
    alert('hello 4');
});

知道吗?

谢谢你的帮助!

onerror中调用hello3函数之前,应该声明它,就像对hello2所做的那样。

演示

修复了jsFiddle,您不应该包装在loader方法(jsFiddl)中并使用.on('error',handler)语法:jsFiddle.net/Ljj68/4(参见对问题的评论,感谢A.Wolff)

最新更新