热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

当在src位置找不到图像时,想要隐藏图像吗?-wanttohideimagewhenimageisnotfoundatthesrclocation?

jQuery(img).bind(error,function(){alert(hi);jQuery(this).hide();
jQuery('img').bind('error',function(){
            alert('hi');
        jQuery(this).hide(); 
        });

I have written this code but non available images are not hiding and still showing cross sign. Can anybody point out what can be wrong. I am writing this under document.ready and i have tried it under window.onload as well.

我已经编写了这段代码,但是没有可用的图像没有隐藏,仍然显示交叉符号。谁能指出哪里出了问题吗?我是在文件下面写的。准备好了,我在窗下试过了。onload。

7 个解决方案

#1


8  

The problem seems to be that by the time you bind your error event, the image's onerror has already fired. You can fire it again by resetting the img.src after binding the event. The following worked on IE8, FF, and Chrome.

问题似乎是,当您绑定错误事件时,映像的onerror已经启动。你可以重新设置img再次发射。绑定事件后的src。下面介绍了IE8、FF和Chrome。

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

$('img').each(function() { this.src = this.src; });

jsFiddle is here: http://jsfiddle.net/7cnQN/

jsFiddle是:http://jsfiddle.net/7cnQN/

#2


2  

Tested in FF, IE, Chrome, Safari, Opera.

测试在FF, IE, Chrome, Safari, Opera。

$('img').each(function() {
    var img = this;
    if (img.complete) {
        if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
            console.log(img, 'load failed');
        } else {
            console.log(img, 'load ok');
        }
    } else if (img.readyState == 'uninitialized') {
        console.log('load failed - IE');
    } else {
        $(img).error(function() { console.log(img, 'load failed - error()'); });
        $(img).ready(function() { console.log(img, 'load ok - onload()'); });
    }
});

#3


0  

On both jQuery(document).ready() and jQuery(window).onload() the img will have already had the error, so you are binding to error too late. Try using:

在jQuery(document).ready()和jQuery(window).onload()中,img都已经有了错误,所以绑定到错误已经太晚了。尝试使用:

jQuery('img').live('error',function(){
            alert('hi');
        jQuery(this).hide(); 
        });

before jQuery(document).ready();

在jQuery(文档)时();

I haven't tried this code yet (and wouldn't have time too for a while) Let me know if it works.

我还没有试过这段代码(暂时也没有时间)让我知道它是否有效。

#4


0  

I personally have issues with JQuery, as it seems to stop people looking into Javascript's native capabilities. I would personally use the onerror event, e.g.:

我个人认为JQuery有问题,因为它似乎阻止了人们研究Javascript的本机功能。我个人会使用onerror事件,例如:

myimage.Onerror= function() { this.style.visibility = 'hidden'; }

模板。Onerror= function() {this.style。可见性= '隐藏';}

(not tested)

(未测试)

#5


0  

The example on the .error() docs show this:

error()文档中的示例显示如下:

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

... which is essentially the same as what you're doing. Can you verify that jQuery is loaded and available on the page? Also, if you're using FireFox, Chrome or Safari, try checking your Javascript console for errors.

…本质上和你所做的是一样的。能否验证页面上是否已加载并可用jQuery ?此外,如果您正在使用FireFox、Chrome或Safari,请尝试检查Javascript控制台是否有错误。

EDIT: Note this also, from the docs:

编辑:注意这一点,来自文档:

This event may not be correctly fired when the page is served locally. Since error relies on normal HTTP status codes, it will generally not be triggered if the URL uses the file: protocol.

当页面在本地服务时,此事件可能不会被正确触发。由于错误依赖于正常的HTTP状态码,因此如果URL使用file: protocol,通常不会触发错误。

Are you testing this locally?

你是在本地测试吗?

#6


0  

You might want to try a different tactic and instead show the images when they load:

您可能想尝试一种不同的策略,而不是在加载时显示图像:

jQuery('img').each(function(i,img){
    jQuery('img').bind('load',function(){ this.show(); });
    if (img.complete)
        jQuery(img).show();
});

First bind to the event, then check to see if the image has already loaded (.complete) and show it. This will require you to hide all of the images before-hand with a script or within the inline style of the individual images. Using a known container as a scope...

首先绑定到事件,然后检查映像是否已经加载(.complete)并显示它。这将要求您预先用脚本或在单个映像的内联样式中隐藏所有映像。使用一个已知的容器作为范围……

jQuery('#container img')

...you can limit this behavior to a subset of the images.

…您可以将这种行为限制为图像的一个子集。

#7


0  

You dealing with a race condition. jQuery may or may not have been loaded when the image is loaded. The following code handles both cases.

你处理的是一个竞态条件。当加载图像时,jQuery可能已经加载,也可能没有加载。下面的代码处理这两种情况。

$('img').each(function() {
    var img = this;
    if (img.complete) {
        if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
            console.log(img, 'load failed');
        } else {
            console.log(img, 'load ok');
        }
    } else {
        $(img).error(function() { console.log(img, 'load failed - error()'); });
        $(img).ready(function() { console.log(img, 'load ok - onload()'); });
    }
});

推荐阅读
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • Java实战之电影在线观看系统的实现
    本文介绍了Java实战之电影在线观看系统的实现过程。首先对项目进行了简述,然后展示了系统的效果图。接着介绍了系统的核心代码,包括后台用户管理控制器、电影管理控制器和前台电影控制器。最后对项目的环境配置和使用的技术进行了说明,包括JSP、Spring、SpringMVC、MyBatis、html、css、JavaScript、JQuery、Ajax、layui和maven等。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 本文介绍了Java工具类库Hutool,该工具包封装了对文件、流、加密解密、转码、正则、线程、XML等JDK方法的封装,并提供了各种Util工具类。同时,还介绍了Hutool的组件,包括动态代理、布隆过滤、缓存、定时任务等功能。该工具包可以简化Java代码,提高开发效率。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
author-avatar
大道废_796
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有