android - 如何处理 phonegap 中的图像损坏或截断错误

标签 android jquery html jquery-mobile cordova

在 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 jQuery documentation -

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/

相关文章:

android - 如何在 Android 中禁用 EditText

javascript - Android (Chrome) 和 playbackRate 属性

基于 Linux 构建的 Android 内核

javascript - 我可以打开一个新窗口并用字符串变量填充它吗?

php - 如何在 paypal 重定向之前提交电子邮件

html - 如何将图像放在文本之上?

javascript - 从文本文件填充 HTML 下拉列表

android - TextView 的文字对齐方式太可怕了!如何解决它?

Javascript 幻灯片滑动

html - 如何在 github markdown 中包含 jsfiddle(或等效的)?