JS學(xué)習(xí)筆記-構(gòu)造一個(gè)畫廊

主函數(shù)main.html

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
    <title>Image Gallery</title>
</head>
<body>
    <!-- 測(cè)試onclick函數(shù) -->
    <!-- <a  onclick="return false">click me</a> -->
    <h1>Snapshots</h1>
    <ul>
        <li><a href="images/Fireworks.jpg" onclick="showPic(this); return false;">Fireworks</a> </li>
        <li><a href="images/Coffee.jpg" onclick="showPic(this); return false;">Coffee</a></li>
        <li><a href="images/Rose.jpg" onclick="showPic(this); return false;">Rose</a></li>
        <li><a href="images/Big Ben.jpg" onclick="showPic(this); return false;">Big Ben</a></li>
    </ul>

    <img id="placeholder" src="images/placeholder.jpg" alt="my imge Gallery">
    <script type="text/javascript" src="scripts/showPic.js"></script>
</body>
</html>

JS代碼showPic.js代碼

function showPic(whichpic){
    var sSource = whichpic.getAttribute("href");
    var oPlaceholder = document.getElementById("placeholder");
    oPlaceholder.setAttribute("src", sSource)
}

說下對(duì)onclick函數(shù)的理解,這個(gè)函數(shù)的返回值默認(rèn)為 true,因此在這里加入return false;用以解決頁(yè)面跳轉(zhuǎn)

學(xué)習(xí)使用childNode方法

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
    <title>Image Gallery</title>
    <link rel="stylesheet" type="text/css" href="css/layout.css" media="screen">
</head>
<body>
    <!-- 測(cè)試onclick函數(shù) -->
    <!-- <a  onclick="return false">click me</a> -->
    <h1>Snapshots</h1>
    <ul>
        <li><a href="images/Fireworks.jpg" title="A fireworks display" onclick="showPic(this); return false;">Fireworks</a> </li>
        <li><a href="images/Coffee.jpg" title="A cup of black Coffee" onclick="showPic(this); return false;">Coffee</a></li>
        <li><a href="images/Rose.jpg" title="A red, red rose" onclick="showPic(this); return false;">Rose</a></li>
        <li><a href="images/Big Ben.jpg" title="The famous clock" onclick="showPic(this); return false;">Big Ben</a></li>
    </ul>

    <img id="placeholder" src="images/placeholder.jpg" alt="my imge Gallery">
    <p id="description" class="lable" title="images"> Choose an image</p>
    <script type="text/javascript" src="scripts/showPic.js"></script>
</body>
</html>

JS代碼部分

function showPic(whichpic){
    var sSource = whichpic.getAttribute("href");
    var sText = whichpic.getAttribute("title");
    var oDescription = document.getElementById("description");
    var oPlaceholder = document.getElementById("placeholder");
    oPlaceholder.setAttribute("src", sSource);
    oDescription.firstChild.nodeValue = sText;
}

CSS代碼部分

body{
    font-family: "Hevetica","Arial",sans-serif;
    color: #333;
    background-color: #ccc;
    margin: 1em 10%;
}

h1{
    color: #333;
    background-color: transparent;
    font-weight: bold;
    text-decoration: none;
}

a{
    color: #c60;
    background-color: transparent;
    font-weight: bold;
    text-decoration: none;
}

ul{
    padding: 0;
}

li{
    float: left;
    padding: 1em;
    list-style: none;
}

p{
    float: left;
}

#placeholder{
    float: left;
}

使用nodeValue獲取到的是文本節(jié)點(diǎn)的文本值

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容