您的位置:首页 > Web前端 > JavaScript

jsp页面上img的onerror属性

2015-11-10 14:17 274 查看
直接贴代码:

<c:choose>
<c:when test="${fn:contains(item.shopPic, 'http://121.40.129.68:8080')}">
<img src="${fn:replace(item.shopPic, 'http://121.40.129.68:8080', 'http://7xlv3q.com2.z0.glb.qiniucdn.com')}" onerror="this.onerror='';this.src='${item.shopPic }'">
</c:when>
<c:otherwise>
<img src="http://7xlv3q.com2.z0.glb.qiniucdn.com${item.shopPic }" onerror="this.onerror='';this.src='${item.shopPic }'">
</c:otherwise>
</c:choose>
if(item.shopPic.indexOf("http://121.40.129.68:8080")>=0){
html += '<img src="'+item.shopPic.replace("http://121.40.129.68:8080","http://7xlv3q.com2.z0.glb.qiniucdn.com") +'" onerror="this.onerror="";this.src="'+item.shopPic +'"">';
}else{
html += '<img src="http://7xlv3q.com2.z0.glb.qiniucdn.com'+item.shopPic +'" onerror="this.onerror="";this.src="'+item.shopPic +'"">';
}

 解决的方法也很简单,在重新给img的src属性赋值时,先将onerror事件清除掉,再赋值,这样就不存在循环调用的问题了,代码段如下:
<img src="http://img1.soufunimg.com/agents/2011_01/08/62/78/sh/houseinfo/408971808300_s22.jpg" 
onerror="this.onerror='';this.src='http://img1.soufunimg.com/agents/2011_01/08/62/78/sh/houseinfo/4089718083001_s.jpg'" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: