用JS 判断IMG 中的图像是否存在? 如果不正确则载入缺省图像 default.gif
<!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> <title> new document </title> <meta name="generator" content="editplus" /> <meta name="author" content="Gao YiXiang" /> <meta name="email" content="yixianggao@126.com" /> <meta name="keywords" content="javascript dhtml dom" /> <meta name="description" content="I love web development." /></head><body> <img id=my src="1.jpg" border="0"> <script type="text/javascript"> // 如果 id为“my”的IMG标签图像不存在,则调入default.gif。怎么写???var defaultImg = "default.gif";var img = document.getElementById("my");// 自定义首次错误标识。img.firstError = true;img.onerror = function(){ if (this.firstError) { this.firstError = false; this.src = defaultImg; } else { this.alt = defaultImg; }};</script></body></html>
页:
[1]