圖片庫程序(動態(tài)創(chuàng)建標記)

圖片庫程序(有誤)

images gallery.html

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Image Gallery</title>
<link rel="stylesheet" href="styles/layout.css" media="screen"/>
</head>
<body>
<h1>Snapshots</h1>
<ul id="imagegallery">
 <li>
  <a href="images/1.jpg" title="katong">katong</a>
 </li>
 <li>
  <a href="images/2.jpg" title="off">off</a>
 </li>
 <li>
  <a href="images/3.jpg" title="a man">a man</a>
 </li>
 <li>
  <a href="images/4.jpg" title="sun">sun </a>
 </li>
 <li>
  <a href="images/5.jpg" title="two head">two head</a>
 </li>
</ul>
<script type="text/javascript" src="scripts/showPic.js"></script>
</body>

layout.css

@charset "utf-8";
/* CSS Document */

body{
    font-family:"Helvetica","Arial",serif;
    color:#333;
    background-color:#ccc;
    margin:1em 10%;
    }
h1{
    color:#333;
    background-color:transparent;
    }
a{
    color:#c60;
    background-color:transparent;
    font-weight:bold;
    text-decoration:none;
    }
ul{
    padding:0;
    }
li{
    float:left;
    padding:1em;
    list-style:none;
    }
img{
    display:block;
    clear:both; 
    }
#imagegallery{
     list-style:none;
    }
#imagegallery{
     display:inline;
    }

showPic.js

function addLoadEvent(func){
     var oldonload=window.onload;
     if(typeof window.onload!='function'){
         window.onload=func;
         }else{
              window.onload=function(){
                   oldonload();
                   func();
                  }
             }
    }
    
function insertAfter(newElement,targetElement){
     var parent=targetElement.parentNode;
     if(parent.lastChild==targetElement){
         parent.appendChild(newElement);
         }else{
              parent.insertBefore(newElement,targetElement.nextSibling);
             }
    }
    
function preparePlaceholder(){
     if(!document.createElement)return false;
     if(!document.createTextNode)return false;
     if(!document.getElementById)return false;
     if(!document.getElementById("imagegallery"))return false;
     
     var placeholder=document.createElement("img");
     placeholder.setAttribute("id","placeholder");
     placeholder.setAttribute("src","images/6.jpg");
     placeholder.setAttribute("alt","my image gallery");
     var description=document.createElement("p");
     description.setAttribute("id","description");
     var desctext=document.createTextNode("Choose an image");
     description.appendChild(desctext);
     var gallery=document.getElementById("imagegallery");
     insertAfter(placeholder,gallery);
     insertAfter(description,placeholder);
     
    }
    



/*function countBodyChildren(){
     var body_element=document.getElementsByTagName("body")[0];
     alert(body_element.nodeType);
    }
    window.onload=countBodyChildren;*/
function prepareGallery(){
    if(!document.getElementsByTagName)return false;     
    if(!document.getElementById)return false; 
    if(!document.getElementById("imagegallery"))return false;   //檢查瀏覽器是否理解getElementByTagName和ById;是否存在id為imagegallery元素
    var gallery=document.getElementById("imagegallery");        //遍歷imagegallery元素中所有鏈接
    var links=document.getElementsByTagName("a");
    for(var i=0;i<links.length;i++){
        link[i].onclick=function(){                             //設置onclick事件,在有關鏈接被點擊時完成操作
            return showPic(this)?false:true;                                      /*把這個鏈接作為參數(shù)傳遞給showPic函數(shù); 如placeholder                                                                                   圖片不存在,瀏覽器按用戶所點擊的鏈接打開一張新圖片*/
            }
            
        }
        
    }
    
    
function showPic(whichpic){
if(!document.getElementById("placeholder"))return false; //d為placeholder是否存在
   var source=whichpic.getAttribute("href");
   var placeholder=document.getElementById("placeholder");
   //if(placeholder.nodeName!="IMG")return false;    //假設是一張圖片,placeholder是否存在
   placeholder.setAttribute("src",source);
      if(document.getElementById("description")){
      var text=whichpic.getAttribute("title")?whichpic.getAttribute("title"):"";    //id為description是否存在(可被忽略)
        
      //title不存在設值為空字符串
         var description=document.getElementById("description");
         if(description.firstChild.nodeType==3){       //檢查文本節(jié)點的nodeType屬性值為3
         description.firstChild.nodeValue=text;
         }
      }
 return true;
}
addLoadEvent(preparePlaceholder);
addLoadEvent(prepareGallery);

頁面效果
![圖片1.png](https://upload-images.jianshu.io/upload_images/13144674-d2e8f7e341390850.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • # 一、框架概述 # 課程概述 1. laravel 4天(之前TP框架還是很大的區(qū)別)(國外框架) 2. 在線教...
    關進一閱讀 491評論 0 0
  • 【綜合】 【辦公文檔】{主標題}政策法規(guī)-人才成長-規(guī)章制度-崗位職責-各項預案-檔案管理-團隊建設{主標題}{主...
    5a15944f1ba2閱讀 499評論 0 0
  • <!DOCTYPE html> Document /* 標簽樣式初始化 */body{margin:0;p...
    三葉松閱讀 858評論 0 2
  • 3月24日一歲九個月的甜心尿濕了褲子,她自己把褲子脫了,奶奶責怪。我說:“寶貝,會脫褲子了,真棒。那我家寶貝也能把...
    潔兒_42cb閱讀 230評論 1 1
  • 這兩天發(fā)生的兩件事,在覺察中的我,才發(fā)覺我的老公和5歲的女兒這么包容和體貼我。 感謝今年有一個閏6月。農(nóng)歷6月是他...
    她喜歡花婆婆閱讀 721評論 0 3

友情鏈接更多精彩內(nèi)容