在 Firefox 的本地主机上工作时出现“图像损坏或截断”错误,但服务器上的同一页面不会产生此类错误。我使用同一页面使用 phonegap 开发 android 应用程序并加载图片我的应用程序被终止,“强制退出”。
我使用以下代码嵌入了图像:-
$("#logo").attr('src', 'url');
而html代码如下:
<img style="width:190px; height: 145px; margin-top: 0%;" id="logo"/>
当只显示一些图片时会出现这样的错误,休息时它工作正常。 请提及处理此类图像错误的方法。
最佳答案
您可以尝试将 jQuery 错误处理程序附加到图像元素。
$('#logo').error(function() {
alert('An error occurred when loading the image!.')
}).attr("src", URL);
您可以将调用链接到 .error()
和 attr()
,这样您就不必执行两个命令。
当该元素上出现 JavaSctipt 错误时,将触发错误回调。
The error event is sent to elements, such as images, that are referenced by a document and loaded by the browser. It is called if the element was not loaded.
关于您的问题的更多相关信息(在本地工作)-
The event handler must be attached before the browser fires the error event, which is why the example sets the src attribute after attaching the handler. Also, the error event may not be correctly fired when the page is served locally; error relies on HTTP status codes and will generally not be triggered if the URL uses the file: protocol.
关于android - 如何处理 phonegap 中的图像损坏或截断错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12106066/