Javascript實現單張圖片瀏覽

來源:互聯網
上載者:User

利用空閑時間,學習JavaScript語言時寫了一個連結瀏覽單張圖片的例子:

 

Code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> Image Gallery </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
 </HEAD>
<script language="javascript">
function showPic(whichpic){
    var source=whichpic.getAttribute("href");//擷取當前點擊的元素的屬性href的值
    var placeholder=document.getElementById("placeholder");//擷取目標
    placeholder.setAttribute("src",source);//設定目標的屬性src,從而達到改變圖片

    var text=whichpic.getAttribute("title");//擷取當前點擊的元素的屬性title的值
    var description=document.getElementById("description");
    description.firstChild.nodeValue=text;//把點擊元素的標題賦給<p>
    //或者 description.childNodes[0].nodeValue=text;
}
</script>
 <BODY>
  <h1>Javascript 圖片館</h1>
  <ul>
    <li>
        <a href="images/fireworks.jpg" title="Image1" onclick="showPic(this); return false;">圖片一</a>
    </li>
    <li>
      <a href="images/coffee.jpg" title="Image2" onclick="showPic(this); return false;">圖片二</a>
    </li>
    <li>
      <a href="images/rose.jpg" title="Image3" onclick="showPic(this); return false;">圖片三</a>
    </li>
    <li>
      <a href="images/bigben.jpg" title="Image3" onclick="showPic(this); return false;">圖片四</a>
    </li>
  </ul>
  <img id="placeholder" src="images/placeholder.gif" alt="my image gallery" />
  <p id="description">選擇圖片.</p>

 </BODY>
</HTML>

 

注意:

在標籤 <a></a> 上加事件我們要注意,避免使它執行跳轉。
解決方案:函數返回false; 事件認為連結沒有被點擊。

如果href的值是javascript:void(0);也可以不跳轉。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.