首页 > 其他 > 详细

图片库最终版

时间:2014-04-16 16:45:18      阅读:519      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
<!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=gb2312" />
<title>查看图片</title>
<style type="text/css">
ul,li{ list-style:none;}
ul li{ line-height:24px; float:left; padding:1em;}
img{ display:block; clear:both;}
</style>
</head>

<body>
<h1>Snapshots</h1>
<ul id=‘imagegallery‘>
    <li><a href="images/1.jpg"  title="pic01111">第一张图片</a></li>
    <li><a href="images/2.jpg"  title="pic02222">第二张图片</a></li>
    <li><a href="images/3.jpg" title="pic03333">第三张图片</a></li>
    <li><a href="images/4.jpg" title="pic04444">第四张图片</a></li>
</ul>
<script>   //此函数写到外部文件里。
addLoadEvent(prepareGallery);
addLoadEvent(preparePlaceholder);


function addLoadEvent(func){
    var oldonload=window.onload;  
    if(typeof window.onload !=function){
        window.onload=func;
    }else{
        window.onload=function(){
            oldonload();
            func();
        }
    }
}
function prepareGallery(){
    if(!document.getElementById ) return false;
    if(!document.getElementsByTagName) 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; 
        }
    // links[i].onkeypress=links[i].onclick;
    }
}
function preparePlaceholder(){
    if(!document.createElement) return false;
    if(!document.createTextNode) return false;
    if(!document.getElementById(imagegallery)) return false;
    var gallery=document.getElementById(imagegallery);
    var placeholder=document.createElement(img);
    placeholder.id=placeholder;
    placeholder.alt=My Image Gallery;
    placeholder.src=images/placeholder.gif;
    var description=document.createElement(p);
    description.setAttribute(id,description);
    var desctxt=document.createTextNode(Choose an image);
    description.appendChild(desctxt);
    //gallery.parentNode.insertBefore(placeholder,gallery);
    //gallery.parentNode.insertBefore(description,gallery);
    insertAfter(placeholder,gallery);
    insertAfter(description,placeholder);

}

function insertAfter(newElement,targetElement){
    var parent=targetElement.parentNode;
    if( parent.lastChild == targetElement ){
        parent.appendChild(newElement);
    }else{
        parent.insertBefore(newElement,targetElement.sibling);
    }
}
function showpic(whichpic){
    if( !document.getElementById(placeholder)) return false;
    var placeholder=document.getElementById(placeholder);
    if(placeholder.nodeName!=IMG) return false;   //*******nodeName属性总是返回一个大写字母的值,即使元素在HTML文档里是小写字母。
    placeholder.setAttribute(src,whichpic.getAttribute(href));
    if(document.getElementById(description)){
        var text=whichpic.getAttribute(title)?whichpic.getAttribute(title): ; //三元操作符
        var description=document.getElementById(description);
        if(description.firstChild.nodeType==3){
          description.firstChild.nodeValue=text;
        }
    }
    return true;
}

</script>
</body>
</html>
bubuko.com,布布扣

 

图片库最终版,布布扣,bubuko.com

图片库最终版

原文:http://www.cnblogs.com/positive/p/3666777.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!