自学《JavaScript DOM编程艺术》,有地方不明白求指点
前段时间买了《JavaScript DOM编程艺术》这本书在自学,到第六章,更新图片库的时候出现问题,我明明是按书上敲的,应该按预想的点击页面上的链接后就在”占位符“图片上切换图片。可是点击链接后却是打开一个新的网页显示图片,请问到底是哪里错了?
function showPic(whichpic){
if(!document.getElementById("placeholder")) return false;
var source=whichpic.getAttribute("href");
var placeholder=document.getElementById("placeholder");
if(placeholder.nodeName!="IMG") return false;
placeholder.setAttribute("src",source);
if(document.getElementById("description")){
var text=whichpic.getAttribute("title") ?whichpic.getAttribute("title"):"";
var description=document.getElementById("description");
if(description.firstChild.nodeType==3){
description.fistChild.nodeValue=text;
}
}
return true;
}
function prepareGallery(){
if(!document.getElementsByTagName) return false;
if(!document.getElementById) return false;
if(!document.getElementById("imagegallery")) return false;
var gallery=document.getElementById("imagegallery");
var links=gallery.getElementsByTagName("a");
for(var i=0;i<links.length;i++){
links[i].onclick=function(){
return showPic(this) ? false:true;
}
}
}
function addLoadEvent(func){
var oldonload=window.onload;
if(typeof window.onload!='function'){
window.onload=func;
}else{
window.onload=function(){
oldonload();
func;
}
}
}
addLoadEvent(prepareGallery);