使用jquery给img绑定error事件
先检查图片是否加载成功,然后如果失败的话再绑定事件
<imgsrc="xxxx.jpg"alt=""/>
<script>
jQuery(document).ready(function(){
jQuery('img').each(function(){
varerror=false;
if(!this.complete){
error=true;
}
if(typeofthis.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');
}
});
});
</script>
相关推荐
84483065 2020-09-11
85477104 2020-08-15
83510998 2020-08-08
tztzyzyz 2020-07-05
delmarks 2020-06-28
89510194 2020-06-27
牵手白首 2020-06-14
EdwardSiCong 2020-11-23
85477104 2020-11-17
hhanbj 2020-11-17
81427005 2020-11-11
seoppt 2020-09-13
honeyth 2020-09-13
WRITEFORSHARE 2020-09-13
momode 2020-09-11
82550495 2020-08-03
tthappyer 2020-08-03
84901334 2020-07-28