图片库程序(动态创建标记)

图片库程序(有误)

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;                                      /*把这个链接作为参数传递给showPic函数; 如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){       //检查文本节点的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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • # 一、框架概述 # 课程概述 1. laravel 4天(之前TP框架还是很大的区别)(国外框架) 2. 在线教...
    关进一阅读 413评论 0 0
  • 【综合】 【办公文档】{主标题}政策法规-人才成长-规章制度-岗位职责-各项预案-档案管理-团队建设{主标题}{主...
    5a15944f1ba2阅读 470评论 0 0
  • <!DOCTYPE html> Document /* 标签样式初始化 */body{margin:0;p...
    三叶松阅读 796评论 0 2
  • 3月24日一岁九个月的甜心尿湿了裤子,她自己把裤子脱了,奶奶责怪。我说:“宝贝,会脱裤子了,真棒。那我家宝贝也能把...
    洁儿_42cb阅读 184评论 1 1
  • 这两天发生的两件事,在觉察中的我,才发觉我的老公和5岁的女儿这么包容和体贴我。 感谢今年有一个闰6月。农历6月是他...
    她喜欢花婆婆阅读 647评论 0 3