• javascript DOM(2) 一个网页上切换显示不同的图片或文本


    摘自: javascript DOM 编程艺术

    1. 在一个网页上切换显示不同的图片

    <!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>Image Gallery</title>
    <script type="text/javascript" src="script/showPic.js"></script>
    </head>
    <body>
    <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/bigben.jpg" title="The famous clock" onclick="showPic(this); return false;">Big Ben</a>
    </li>
    </ul>
    <img id="placeholder" src="images/placeholder.gif" alt="my image gallery">
    <p id="description">Choose a image.</p>
    </body>
    </html>

    showPic.js

    function showPic(whichpic) {

    var source = whichpic.getAttribute("href");
    var placeholder = document.getElementById("placeholder");
    placeholder.setAttribute("src",source);
    }

    2.在同一个网页上切换显示不同的文本

    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;
    }

  • 相关阅读:
    .Net魔法堂:log4net详解
    CentOS6.5菜鸟之旅:安装SUN JDK1.7和Tomcat7
    Java魔法堂:注释和注释模板
    Eclipse魔法堂:任务管理器
    CentOS6.5菜鸟之旅:VirtualBox4.3识别USB设备
    Windows魔法堂:解决“由于启动计算机时出现页面文件配置问题.......”
    JS魔法堂:IE5~9的Drag&Drop API
    CentOS6.5菜鸟之旅:安装VirtualBox4.3
    HTML5魔法堂:全面理解Drag & Drop API
    byzx
  • 原文地址:https://www.cnblogs.com/peterli2013/p/3183948.html
Copyright © 2020-2023  润新知