Javascript 第六章

本文介绍了一种使用HTML、CSS和JavaScript实现的图片快照与画廊展示方法,通过链接图片并添加点击事件,用户可以轻松查看和选择图片。
<html>
 <head>
     <meta charset=utf-8>   
     <title></title>
 </head>
 <body>
 <h1>Snapshots</h1>
 <ul>
     <li><a href="images/1 (1).png" title="picture1" alt="picture1">picture1</a></li>
     <li><a href="images/1 (2).png" title="picture2" alt="picture2">picture2</a></li>
     <li><a href="images/1 (3).png" title="picture3" alt="picture3">picture3</a></li>
 </ul>
 
 <img src="images/placeholder.png" alt="my image gallery" id="placeholder">
 <p id="description">Choose an image</p>
 
 
 <script>

 function addLoadEvent(func){
        var oldonload = window.onload;
        if (typeof oldonload != 'function') {
            window.onload = func;
        }else{
            window.onload = function(){
                oldonload();
                func();
            }
        }
    }


 function prepareGallery(){    
     if (!document.getElementsByTagName || !document.getElementById) return false;    
     links = document.getElementsByTagName("a");
     for (var i = 0,j = links.length;i<j; i++) {
         links[i].onclick = function(){
return !showPic(this); } } } 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"); var description = document.getElementById("description"); if (description.firstChild.nodeType == 3) { description.firstChild.nodeValue = text; } } return true; } addLoadEvent(prepareGallery); </script> </body> </html>

 

转载于:https://www.cnblogs.com/mguo/archive/2013/03/11/2954131.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值