JavaScript學(xué)習(xí)筆記之圖片庫案例分析
本文實例講述了JavaScript圖片庫。分享給大家供大家參考,具體如下:
一、一個javascript 圖片庫實例,下面是效果圖
點擊頂部導(dǎo)航,會在本頁面進(jìn)行刷新圖片,然后,在底部會顯示文本的變化
二、下面是代碼
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;">藍(lán)色楓葉</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 元素的子節(jié)點集合:
document.body.childNodes;
2、nodeType
獲得 body 元素的節(jié)點類型:
document.body.nodeType;
3、nodeValue
nodeValue 屬性設(shè)置或返回指定節(jié)點的節(jié)點值。
4、firstChild、lastChild
firstChild 屬性返回指定節(jié)點的首個子節(jié)點,以 Node 對象。
lastChild 屬性返回指定節(jié)點的最后一個子節(jié)點,以 Node 對象。
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript操作DOM技巧總結(jié)》、《JavaScript頁面元素操作技巧總結(jié)》、《JavaScript事件相關(guān)操作與技巧大全》、《JavaScript查找算法技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript錯誤與調(diào)試技巧總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章
javascript級聯(lián)下拉列表實例代碼(自寫)
javascript下拉菜單想必大家在瀏覽網(wǎng)頁的時候都會看到吧,已不是那么陌生了,本文介紹使用javascript實現(xiàn)級聯(lián)下拉列表實例,感興趣的朋友可以參考下哈,希望對你有所幫助2013-05-05借助JavaScript腳本判斷瀏覽器Flash Player信息的方法
做了一個小的Demo,在測試時發(fā)現(xiàn)經(jīng)常報錯,對此總結(jié)了一下借助JavaScript腳本判斷瀏覽器Flash Player信息的方法,需要的朋友可以參考下2014-07-07javascript實現(xiàn)在指定元素中垂直水平居中
當(dāng)談到網(wǎng)頁的布局中,居中問題一直得不到很有效的解決,居中通常是相對于某一個元素的,比如我們經(jīng)常所說的屏幕居中的問題,我們了解父元素的信息越多,我們就越能更加容易的實現(xiàn)居中布局。下面我們通過具體的實例來看看javascript如何來實現(xiàn)垂直水平居中2015-09-09