zoukankan      html  css  js  c++  java
  • Javascript使用案例(1)

    在网页上实现图片与文字的替换

    运用DOM的五个常用方法

    1.getElementById;

    2.getElementsByTagName;

    3.getElementByClassName;

    4.getAttribute;

    5.setAttribute;

    来进行图片的替换

     whichpic.getAttribute("href");
     var source = whichpic.getAttribute("href");
    /* document.getElementById("placeholder");*/
     var placeholder = document.getElementById("placeholder");
     placeholder.setAttribute("src",source);
    具体实现js代码。

    进行文字的替换

    DOM属性

    1.childNodes; 

    2.nodeType;

    3.nodeValue;

    4.firstChild;

    5.lastChild;

    var text = whichpic.getAttribute("title");
    var description = document.getElementById("description");
    description.firstChild.nodeValue = text;
    实现代码。

    整体代码

    html部分

    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <title>Image Gallery</title>
        <link href="layout.css" type="text/css" rel="stylesheet" media="screen">
    </head>
    <body>
    <h1>Snapshots</h1>
    <ul>
        <li>
            <a href="11.jpg" onclick="showPic(this); return false;" title="first lady">First Lady</a>
        </li><!--添加return false 是防止点击事件出发后被转到原来界面-->
        <li>
            <a href="12.jpg" onclick="showPic(this); return false;" title="second lady">Second Lady</a>
        </li>
        <li>
            <a href="13.jpg" onclick="showPic(this); return false;" title="third lady">Third Lady</a>
        </li>
        <li>
            <a href="14.jpg" onclick="showPic(this); return false;" title="forth lady">Forth Lady</a>
        </li>
    
    </ul>
    <img id="placeholder" src="15.jpg" alt=" my image gallery">
    <p id="description">Choose an image.</p>
    <script  type="text/javascript"  src="01.js"></script>
    </body>
    </html>
    js部分

    /**
     * Created by Administrator on 2015/9/1.
     */
    function showPic(whichpic){
        whichpic.getAttribute("href");
        var source = whichpic.getAttribute("href");
       /* document.getElementById("placeholder");*/
        var placeholder = document.getElementById("placeholder");
        placeholder.setAttribute("src",source);
        var text = whichpic.getAttribute("title");
        var description = document.getElementById("description");
        description.firstChild.nodeValue = text;
    }
    
    function countBodyChildren(){
        var body_element = document.getElementsByTagName("body")[0];
    /*    alert(body_element.childNodes.length);*/
        alert(body_element.nodeType);
    }
    /*window.onload = countBodyChildren;*/
    
    css美化部分

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

  • 相关阅读:
    vs code python 关于无法找到文件路径的问题 No such file or directory
    vs2017 c# 控制台 输出中文显示问号 ; vs2017 c# 控制台 输出中文显示乱码
    web页面实现指定区域打印功能
    html 实现动态在线预览word、excel、pdf等文件(方便快捷)
    vuetify使用时遇到的坑:默认颜色显示不了
    【VS Code】中node.js代码自动补全的方法
    vue-property-decorator使用指南
    关于webpack,babel,以及es6和commonJS之间的联系(转)
    tslint.json的配置项说明
    [TypeScript] vs code TSLint常见错误解决方案
  • 原文地址:https://www.cnblogs.com/mxdneu/p/5203757.html
Copyright © 2011-2022 走看看