我需要能够检测图像是否被破坏,并在图像链接断开时替换为默认图像.我知道我可以使用图像代理执行此操作,但希望能够使用javascript动态执行此操作.
您可以使用<img onerror='doWhateverFunction()'等
http://msdn.microsoft.com/en-us/library/cc197053(v=VS.85).aspx
任何事件onerror都会在DOM上向上传播,因此您可以为此类错误创建一个通用处理程序.
<script type="text/javascript">
jQuery(document).bind('error', function(event) {
console.log(event.target.src);
});
</script>
Run Code Online (Sandbox Code Playgroud)