您好,欢迎访问一九零五行业门户网

利用图片的 onerror 事件载入默认图片_javascript技巧

当图片不存在时载入默认图片的代码
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=gb2312" /> 
<title>载入默认图片</title> 
</head>
<body> 
<div id="container"></div> 
<script language="javascript"> 
<!--
window.onload=function(){
var container = document.getelementbyid("container");
var thumbimg = container.appendchild(document.createelement('img'));
thumbimg.src = "20091214920298336.jpg" ; //不存在的图片
thumbimg.onerror = function(){thumbimg.src='http://www.jb51.net/images/logo.gif';};
}
//--> 
</script> 
</body> 
</html>
其它类似信息

推荐信息