本文實例講述了JavaScript圖片庫。分享給大家供大家參考,具體如下:
一、一個javascript 圖片庫實例,下面是效果圖
點擊頂部導航,會在本頁面進行刷新圖片,然后,在底部會顯示文本的變化
二、下面是代碼
1、gallery.html代碼
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="js/showPic.js"></script> <link rel="stylesheet" type="text/css" href="css/layout.css" rel="external nofollow" /> </head> <body> <h1>Snapshots</h1> <ul> <li> <a href="img/a.jpg" rel="external nofollow" title="Hongse Fengye" onclick="showPic(this);return false;">紅色楓葉</a> </li> <li> <a href="img/b.jpg" rel="external nofollow" title="Huangse Fengye" onclick="showPic(this); return false;">黃色楓葉</a> </li> <li> <a href="img/c.jpg" rel="external nofollow" title="Hongse Shu" onclick="showPic(this); return false;">紅色樹</a> </li> <li> <a href="img/d.jpg" rel="external nofollow" title="Lanse Fengye" onclick="showPic(this);return false;">藍色楓葉</a> </li> </ul> <img src="img/3302-106.jpg" id="placeholder" alt="My Gallery"/> <p id="description">Choose an image</p> </body></html>
2、showPics.js代碼
function showPic(whichpic){ var sorce=whichpic.getAttribute("href"); var placeholder=document.getElementById("placeholder"); placeholder.setAttribute("src",sorce); var text=whichpic.getAttribute("title"); var description=document.getElementById("description"); description.firstChild.nodeValue=text;}
3、layout.css代碼
img{ width: 600px;}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;}
三、幾個新的DOM屬性
1、childNodes
獲得 body 元素的子節點集合:
document.body.childNodes;
2、nodeType
獲得 body 元素的節點類型:
document.body.nodeType;
3、nodeValue
nodeValue 屬性設置或返回指定節點的節點值。
4、firstChild、lastChild
firstChild 屬性返回指定節點的首個子節點,以 Node 對象。
lastChild 屬性返回指定節點的最后一個子節點,以 Node 對象。
更多關于JavaScript相關內容感興趣的讀者可查看本站專題:《JavaScript操作DOM技巧總結》、《JavaScript頁面元素操作技巧總結》、《JavaScript事件相關操作與技巧大全》、《JavaScript查找算法技巧總結》、《JavaScript數據結構與算法技巧總結》、《JavaScript遍歷算法與技巧總結》及《JavaScript錯誤與調試技巧總結》
希望本文所述對大家JavaScript程序設計有所幫助。
新聞熱點
疑難解答