jsp页面上img的onerror属性

直接贴代码:

<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'" />


你可能感兴趣的:(img,onerror属性)