第七章 點擊鏈接換圖片

<!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=utf-8" />
<title>無標題文檔</title>
<script  type="text/javascript">
 function atlas(){
  var links = document.getElementsByTagName("a");
  var newImg = document.createElement("img");
  newImg.setAttribute("src","images/犬夜叉.jpg");
  newImg.setAttribute("id","image");
  newImg.setAttribute("alt","my Image gallery");
  var para = document.createElement("p");
  para.setAttribute("id","para");
  var text = document.createTextNode("站位圖");
  para.appendChild(text);
  for(var i=0; i<links.length;i++){
   links[i].onclick = function(){
    var href = this.getAttribute("href");
 newImg.setAttribute("src",href);
    para.childNodes[0].nodeValue = this.getAttribute("title");
 return false;                                                                                                                                                       
   }
 
  }
  insterAfter(newImg,imageGallery);
  insterAfter(para,newImg) ;
 };
 
function insterAfter(newElement,targetElement){
  var parent = targetElement.parentNode;
  if(parent.lastChild == targetElement){
   parent.appendChild(newElement);
  }else{
   parent.insertBefore(newElement,targetElement.nextSibling);
  }
};
window.onload = atlas;
</script>
</head>

<body>
 <div id="imageGallery">
  <div><a href="images/海賊王.jpg" title="海賊王">海賊王</a></div>
  <div><a href="images/火影.jpg" title="火影">火影</a></div>
  <div><a href="images/柯南.jpg" title="柯南">柯南</a></div>
  <div><a href="images/龍珠.jpg" title="龍珠">龍珠</a></div>
</div>
<div>ppp</div>
</body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章