< script > ... document.onclick = function() ...{ validateElement(event.srcElement); return false;}function validateElement(o) ...{ //验证对象类型 if (!(o.tagName == 'a' || o.tagName == 'A')) ...{ return; } //初始化验证 var url = o.href; var img = document.createElement('img'); //img.style.display = 'none'; img.src = url; img.onerror = function() ...{ alert('error!'); imgExists = false; img.outerHTML = ""; }; document.body.appendChild(img); //验证地址 var imgExists = validateImg(img); alert(imgExists); //document.body.removeChild(img); //决定操作 //如果图片不存在,打开指定网址 if (!imgExists) ...{ o.href = 'http://www.g.cn'; } alert(o.href);}validateElement.prototype.prvElm = '';function validateImg(img) ...{ var imgExists = true; return imgExists;} </ script > < a href ="http://bbs.51js.com/images/default/logo.gifd" target ="_blank" > click me </ a >