php img注册事件,jquery给img绑定error事件的问题解决

如:xxxx.jpg

$('img').error(function(){

$(this).attr('src',"默认图片");

})

经过测试发现,如果原始图片不存在的话,页面上的图片就会一直闪烁,如何解决这个问题?$(window).load(function() {

$('img').each(function() { if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {

this.src = 'http://www.tranism.com/weblog/images/broken.gif';

}

});

});

没太懂,原始图片是指xxxx.jpg吗?你这个代码如果默认图片路径也是错误的话(也就是默认图片也不存在)就进入死循环了,所以一直闪啊闪,因为不断的onerror

先检查图片是否加载成功,然后如果失败的话再绑定事件。而且替换一次就好了。

jQuery('img').each(function(){ var error = false; if (!this.complete) {

error = true;

} if (typeof this.naturalWidth != "undefined" && this.naturalWidth == 0) {

error = true;

} if(error){

$(this).bind('error.replaceSrc',function(){ this.src = "default_image_here.png";

$(this).unbind('error.replaceSrc');

}).trigger('load');

}

});

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值