JavaScript學習系列3 案例研究: JavaScript圖片庫

 <!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=utf-8" />
<title>無標題文檔</title>
</head>
<body>
     <h1>Snapshots</h1>
    <ul>
         <li>
             <a href="p_w_picpaths/Chrysanthemum.jpg" return false;" title="A Chrysanthemum display">Chrysanthemum</a>
        </li>
        <li>
             <a href="p_w_picpaths/Desert.jpg" return false;" title="A Desert display">Desert</a>
        </li>
        <li>
                 <!-- 點擊的時候調用showPic(this)方法,這裏this爲當前節點、return false: 說明這個鏈接沒有被點擊 -->
             <a href="p_w_picpaths/Hydrangeas.jpg" return false;" title="A Hydrangeas display">Hydrangeas</a>
        </li>
        <!-- 在下方顯示圖片 -->
        <img id="placeholder" src="" alt="my p_w_picpath gallery" />
        <!-- 通過id更改字體 -->
        <p id="description">Choose an p_w_picpath.</p>
    </ul>
     
     <script src="file.js"></script>
</body>
</html>
 
JavaScript
function showPic(whichpic) {
     var source = whichpic.getAttribute("href");
     var placeholder = document.getElementById("placeholder");
     placeholder.setAttribute("src", source);
     var text = whichpic.getAttribute("title");
     var description = document.getElementById("description");
     description.firstChild.nodeValue = text;
}
 
  • childNodes: 獲取子節點的數組
  • nodeType: 節點類型
    • 元素節點: 1
    • 屬性節點: 2
    • 文本節點: 3
  • nodeValue: 節點的值
  • firstChild: 第一個子節點
  • lastChild: 最後一個子節點

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章